Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Response Interface

Vue.js Response Interface

Vue can add a dynamic data response interface.

For example, in the following instance we use the $watch property to monitor data; $watch must be added outside the Vue instance to achieve the correct response.

In the example, clicking the button increments the counter by 1. setTimeout sets the counter value to increase by 20 after 10 seconds.

Example

<div id="app">
<p style=">

font-size:25px;

"

Counter: {{ counter }}

p

<button@click="counter++" style=">

font-size:25px;

"

Click me

button

div

<script type="text/javascript">

var vm = new Vue({ el: '#app', data: { counter: 1 } }); vm.$watch('counter', function(nval, oval) { alert('Counter value changed :' + oval + ' changed to ' + nval + '!'); }); setTimeout( function(){ vm.counter += 20; },10000 );

script

Try it »

Vue does not allow dynamically adding new root-level reactive properties to an already created instance.

Vue cannot detect the addition or deletion of object properties; the best approach is to declare root-level reactive properties before initializing the instance, even if just with an empty value.

If we need to add or remove properties at runtime, we can use the global Vue, Vue.set, and Vue.delete methods.

Vue.set

The Vue.set method is used to set an object's property; it solves the limitation that Vue cannot detect added properties. The syntax is as follows:

Vue.set( target, key, value )

Parameter Description:

Example

<div id="app">
<p style=">

font-size:25px;

"

Counter: {{ products.id }}

p

<button@click="products.id++" style=">

font-size:25px;

"

Click me

button

div

<script type="text/javascript">

var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { products: myproduct } }); vm.products.qty = "1"; console.log(vm); vm.$watch('products.id', function(nval, oval) { alert('Counter value changed :' + oval + ' changed to ' + nval + '!'); });

script

Try it »

In the above example, the following code was used to create a variable myproduct at the start:

var myproduct = {"id":1, name:"book", "price":"20.00"};

This variable is assigned to the data object of the Vue instance:

var vm = new Vue({ el: '#app', data: { products: myproduct } });

If we want to add one or more properties to the myproduct array, we can use the following code after the Vue instance is created:

vm.products.qty = "1";

Check the console output:

As seen in the figure above, the quantity property qty was added to the product, but the get/set methods are only available for the id, name, and price properties, and cannot be used on the qty property.

We cannot achieve reactivity by adding a Vue object. Vue mainly creates all properties at the start. To achieve this, we can use Vue.set:

Example

<div id="app">
<p style=">

font-size:25px;

"

Counter: {{ products.id }}

p

<button@click="products.id++" style=">

font-size:25px;

"

Click me

button

div

<script type="text/javascript">

var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { products: myproduct } }); Vue.set(myproduct, 'qty', 1); console.log(vm); vm.$watch('products.id', function(nval, oval) { alert('Counter value changed :' + oval + ' changed to ' + nval + '!'); });

script

Try it »

From the console output, we can see that the get/set methods are available for the qty property.

Vue.delete

Vue.delete is used to delete dynamically added properties. Syntax:

Vue.delete( target, key )

Parameter Description:

Example

<div id="app">
<p style=">

font-size:25px;

"

Counter: {{ products.id }}

p

<button@click="products.id++" style=">

font-size:25px;

"

Click me

button

div

<script type="text/javascript">

var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { products: myproduct } }); Vue.delete(myproduct, 'price'); console.log(vm); vm.$watch('products.id', function(nval, oval) { alert('Counter value changed :' + oval + ' changed to ' + nval + '!'); });

script

Try it »

In the above example we used Vue.delete to delete the price property. The following is the console output:

From the output in the figure above, we can see that the price property has been deleted, leaving only the id and name properties, and the get/set methods of the price property have also been removed.