效果如下:
<head>
</head>
<body>
<div id="myvue">
<span>用户名</span>
<!--model.lazy在input失去焦点后绑定-->
<input type="text" v-model.lazy="username">
<span>{{tip}}</span>
</div>
<script src="js/vue.js"></script>
<script>
var myvue=new Vue({
el:'#myvue',
data:{
username:'',
tip:''
},
methods:{
//这里应该是调用接口,但是可以使用定时任务的方式模拟接口调用
changeUn:function(username){
var that=this;
setTimeout(function(){
if(username=='admin'){
that.tip='该用户名已使用,请更换!'
}else{
that.tip='用户名可以使用'
}
},2000)
}
},
watch:{
username:function(val){
//调用后台接口验证用户名的合法性
this.changeUn(val);
//修改提示信息
this.tip='正在验证……'
}
}
});
</script>
</body>