vue watch

2021-03-02 15:45:34 浏览数 (1)

代码语言:javascript复制
格式:
a.$watch(数据,function(){
	
})

代码:

代码语言:javascript复制
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<script type="text/javascript" src='js/bower_components/vue/dist/vue.js'></script>
	<title></title>
</head>
<body>
	<input type='button' value='改变了' name="" @click='changed' >
	<script type="text/javascript">
		var a = new Vue({
			el:'body',
			data:{
				a:10,
				b:20
			},
			methods:{
				changed:function(){
					this.a = this.a 1;
				}
			}
		});
		a.$watch('a',function()
		{
			console.log("改变了.");
		});
	</script>
</body>
</html>

解释:

只有真真发生改变了watch才会触发. 注意: 运算过程后如果还是原来的值也不触发.

效果:

0 人点赞