How to declare a temporary variable in vueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

Today, I am going to show you How do you declare a temporary variable in vueJS.

Vue. set is a tool that allows us to add a new property to an already reactive object and makes sure that this new property is ALSO reactive.

Table of Content

  • Setup the Vue (Optional)
  • Define the variable
  • Access the veritable

This article will guide you to how do I define the temporary variable in vueJS.

Step 1: Setup the Vue (Optional)

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation. How to Setup the VueJS project?

Step 2: Define the variable

In Component <center></center> tag is used to initialize the heading in the center position. The heading :set=" is used to define the new temporary variable as a property in the Html property in the template.

<template>
  <div>
    <center>
      <h1 :set="title='Hi Friends, Welcome To SortoutCode!'">{{title}}</h1>
    </center>
  </div>
</template>

Step 3: access the variable

we can access the temporary variable in methods using the this keyword. For Example use, we can access the title variable in methods this. title.

// I am going to access the title in methods
mounted(){
  this.title;  // Hi Friends, Welcome To SortoutCode!
}

For now, let’s check the output.

Output

Temporary Variable

All the best 👍.

Follow me on Twitter