Author -  Sai gowtham

Intro to Methods and data properties in Vuejs

In this tutorial, we are going to learn about how to use methods and data properties in Vue JS.

For this tutorial we are using Vue cli to generate our Project.

Creating our project

Let’s quickly create our vue project by running the following command.

vue create vue-app

This above command will download required files in vue-app folder.

Note: if you don’t about how to create vue project using cli then check out my Vue Cli 3 tutorial

Change your working directory by using cd vue-app then open your project folder by using your favorite code editor.

Inside your src folder navigate to App.vue file and remove everything then replace with below code.

App.vue
<template>
  <div>
    <h1>Hello Vuejs</h1>
  </div>
</template>

<script>
export default {};
</script>

Data property

In the above code, we are exporting default empty object inside the <script> tag let’s add the data property to that empty object.

<script>
export default {
   data:function(){
       return {
           title: "Vuejs"       }
   }
};
</script>

data: The data property value is an anonymous function which is returning object.Every property inside that object is added to the Vue reactivity system so that if we change that property value then vuejs re-renders the dom with the updated data.

let’s add the title property to our template tag by using {{}} double curly braces.

App.vue
<template>
  <div>
    <h1>Hello {{title}}</h1>  </div>
</template>

<script>
export default {
  data: function() {
    return {
      title: "Vuejs"
    };
  }
};
</script>

In Vuejs we need to use double curly braces {{js expression }} to pass the JavaScript expressions.

Let’s start the development server by using the following command.

npm run serve

vuejs-data-interpolation

Have you seen our {{title}} is replaced with Vuejs.

Methods property

The name denotes you can create methods by using me, let’s create our first method by using methods property.

The methods property value is also an object.

<script>
export default {
  data: function() {
    return {
      title: "Vuejs"
    };
  },
  methods:{
      welcomeMsg:function(){          return `Welcome to ${this.title} world`      }  }
};
</script>

In the above code, we have added a welcomeMsg method which is returning a string.

Inside the methods, we can access the data object by using this.propertyname.

Let’s invoke the welcomeMsg method inside our template tag.

<template>
  <div>
    <h1>Hello {{title}}</h1>
    <h2>{{welcomeMsg()}}</h2>  </div>
</template>

vuejs-data-adding-js-expressions

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY