<!doctype html>
<html lang="gbk">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>VUE 监听 watch</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="num" style="width:30px">的平方是{{result}}
</div>
<script>
new Vue({
el:'#app',
data:{
num:1
},
computed:{
result(){
return this.num* this.num;
}
}
});
</script>
</body>
</html>
或者使用如下:
<script>
var vm = new Vue({
el:'#app',
data:{
num:1,
result:1
}
});
vm.$watch("num",function(newValue,oldValue){
console.log(newValue,oldValue);
this.result = newValue*newValue;
})
</script>
或者
<script>
new Vue({
el:'#app',
data:{
num:1,
result:1
},
watch:{
num(newValue,oldValue){
console.log(newValue,oldValue);
this.result = newValue*newValue;
}
}
});
</script>