Vue中的数据监听和数据交互案例解析
现在我们来看一下vue中的数据监听事件$watch,
js代码:
newVue({
el:"#div",
data:{
arr:[1,2,3]
}
}).$watch("arr",function(){
alert("数据改变了")
})
html代码:
{{arr}}
这就是数组的监听,对于json我们也是一样的,但是我们得给他一个深度监听,$watch的第三个参数{deep:true}。
angular中的数据交互有$http,同样对于vue我们也是有数据交互的,有post,get以及jsonp的方法。
我们在这里做一个简单的百度搜索功能
css代码:
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:1pxsolidgray;
list-style:none;
width:500px;
}
js代码:
newVue({
el:"#div",
data:{
msg:"",
arr:[]
},
methods:{
get:function(){
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?',{
wd:this.msg
},{
jsonp:'cb'
}).then(function(res){
this.arr=res.data.s
},function(s){
console.log(s);
});
}
}
})
html代码:
{{item}}
这样一个简单的小案例就做好了。
以上所述是小编给大家介绍的Vue中的数据监听和数据交互案例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!