Vue侦听器
程序员文章站
2022-05-16 21:36:41
...
Vue侦听器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
用户名:<input type="text" v-model.lazy="msg"><span>{{ tips }}</span>
</div>
<script>
var vm=new Vue({
el:"#app",
data:{
msg:"",
tips:""
},
methods: {
handelmeout:function(val){
var that=this;//因为setTimeout里面也有this 所以要分开使用
setTimeout(function(){
if(val==="admin"){
that.tips="用户名已经存在";
}else{
that.tips="用户名可用";
}
},2000)
}
},
//侦听器 侦听msg
watch: {
msg:function(val){
this.handelmeout(val);
this.tips="正在验证....";
}
},
})
</script>
</body>
</html>
上一篇: C# partial 关键字详解