<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
<span>用户名:</span>
<input type="text" v-model.lazy='uname'>
</div>
<span>{{tip}}</span>
</div>
<script type="text/javascript" src="./js/vue.js"></script>
<script>
/* 侦听器 监听用户变化*/
var vm = new Vue({
el: '#app',
data: {
uname: '',
tip: ''
},
methods: {
checkName: function(uname) {
//模拟接口
var that = this;
setTimeout(function() {
if (uname == 'admin') {
that.tip = '用户名已经存在'
} else {
that.tip = '用户名可以使用'
}
}, 2000)
}
},
watch: {
uname: function(val) {
//使用后后台验证用户名的合法性
this.checkName(val);
//修改名字
that.tip = '正在验证..'
}
}
})
</script>
</body>
</html>