现在我们来看一下vue中的数据监听事件$watch,
js代码:
new Vue({
el:"#div",
data:{
arr:[1,2,3]
}
}).$watch("arr",function () {
alert("数据改变了")
})html代码:
<div id="div">
<input type="button" value="改变" @click="arr.push(5)">
<h1>{{arr}}</h1>
</div>这就是数组的监听,对于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:1px solid gray;
list-style: none;
width: 500px;
}
js代码:
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代码:
<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>这样一个简单的小案例就做好了。
以上就是vue中有关数据监听与数据交互的小例子的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号