Now let’s take a look at the data monitoring event $watch in vue,
js code:
new Vue({ el:"#div", data:{ arr:[1,2,3] } }).$watch("arr",function () { alert("数据改变了") })
html code:
<div id="div"> <input type="button" value="改变" @click="arr.push(5)"> <h1>{{arr}}</h1> </div>
This is the monitoring of the array. We are the same for json, but we have to give it a deep monitoring, the third parameter of $watch is {deep: true}.
The data interaction in angular includes $http. Similarly for vue, we also have data interaction, including post, get and jsonp methods.
We are doing a simple Baidu search function here
css code:
a{ text-decoration: none; color: black; } #div{ text-align: center; padding-top: 50px; } input{ height: 25px; width: 500px; border-radius: 5px; outline: none; } ul{ margin-left:470px; margin-top: 0; } li{ height: 25px; text-align: left; border:1px solid gray; list-style: none; width: 500px; }
js code:
new Vue({ el:"#div", data:{ msg:" ", arr:[] }, methods:{get:function () {this.$http.jsonp('',{ wd:this.msg },{ jsonp: 'cb'}).then(function(res){this.arr=res.data.s },function(s){ console.log(s); }); } } })
html Code:
<div id="div"> <input type="text" v-model="msg" @keyup="get()"> <ul> <li v-for="item in arr"><a href="javascript:;">{{item}}</a></li> </ul> </div>
Such a simple small case is ready.
The above is the detailed content of A small example of data monitoring and data interaction in vue. For more information, please follow other related articles on the PHP Chinese website!