Vue.js Reactive Interface

Facebooktwitterredditpinterestlinkedinmailby feather

Vue.js Reactive Interface

Vue reactive interface is one of the important features of the Vue.js framework. Vue.js offers an option to apply the reactivity interface on the properties that are dynamically added. Now, we are going to create a Vue Instance, and then add a watch property to the Vue instance.

Example: This example explains how we can use the Vue.js reactivity interface in the Vue.js application.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Reactive Interface

Now, it is required to use the $watch property to add watch outside the Vue instance. Here, we added an alert message that shows the value changes to the item property. Also added a timer function i.e., setTimeout function, which sets the item value to 25, and time is set for 2500 milliseconds.

Vue.js Reactive Interface

In the above screenshot, we can see the number of items with the “Click Here” button and also saw an alert message. When we click on the “Ok” button of alert message, it increases the number of items, as shown below:

Vue.js cannot identify the property of addition and deletion. The best way to classify the property is always to define the properties, which are required to be reactive upfront in the Vue instance. If we want to add the properties at run time, we can use the Vue.set and Vue.delete methods.

There are various methods of the Vue reactivity Interface, which is very useful in the Vue.js application. Some of the popular methods are as follows:

Vue.set

The vue.set method is used to set an attribute on an object. Vue.js cannot detect the addition or deletion property in the Vue component. It is because Vue conducts the conversion process of getter/setter during the initialization of instances. A getter/setter property must be available in the object of data in order for Vue to convert it and make it reactive. We can have the following syntax to use the vue.set method:

Syntax:

Here the target, key, and value are used for:

Target: It could be an object or an array.

Key: It could be a string or a number.

Value: It could be of any type.

Example: This example explains how we can use the vue.set method in the Vue.js application.

Index.html

Main.js

Output: After executing this code, we will get output, as shown below:

Vue.js Reactive Interface

In the above screenshot, we can see that there is an item quantity, which is 2. We can also see this item quantity in the console. In the console, we can see the get/set method, which is available for the id, name, and price, but these methods are not available for the quantity.

Now, let’s take the same example using quantity, id, name, and price.

Example:

Main.js

Output: After executing this code, we will get the output, as shown below;

Vue.js Reactive Interface

In the above screenshot, we can see all the details about the item like as id, price, name, and quantity.

Vue.delete

The Vue.delete method is used to dynamically delete the property. We can have the following syntax to use the vue.delete method:

Syntax:

Here the target and key are used for:

Target: It could be an object or an array.

Key: It could be a string or a number.

Example: This example explains how we can use the vue.js delete method in the Vue.js application.

Index.html

Main.js

Output: After executing this code, we will get the output, as shown below:

Vue.js Reactive Interface

In the above example, we can see that how we can delete the data from an array. Here, the vue.delete method is used to delete the name from the array.

After deletion, we can see only the id and price in the console, as the name is deleted from the array.

Facebooktwitterredditpinterestlinkedinmailby feather