第一个浅度监听:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>

	<body>
		<div id="app">
			<p>{{a}}</p>
			<p>{{b}}</p>
		</div>

		<script>
			
		var vm=new Vue({
				el:"#app",
				data:{
					a:10,
					b:15
				}
		});
		vm.$watch("a",function(){
			alert('a变化了');
			this.b=100;
		});
		document.οnclick=function(){
			vm.a=2
		}
			
			
		</script>
	</body>
</html>
第二个深度监听

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>

	<body>
		<div id="app">
			<p>{{a|json}}</p>
			<p>{{b}}</p>
		</div>

		<script>
			var vm = new Vue({
				el: "#app",
				data: {
					a: { id: "1", title: "width" },
					b: 15
				}
			});
			vm.$watch("a", function() {
				alert('a变化了');
				this.b = 100;
			}, { deep: true });
			document.onclick = function() {
				vm.a.id = "2";
			}
		</script>
	</body>

</html>



Logo

前往低代码交流专区

更多推荐