Vue计算属性(computed)和侦听器(watch)的区别

		<div id="app">
			{{fullName}}
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el: '#app',
				data:{
					firstName:'Stepon',
					lastName:'Chou',
					fullName:'ChouXinci'
				},
				watch:{
					firstName(val){
						this.fullName = val + ' ' + this.lastName
					},
					lastName(val){
						this.fullName = this.firstName + ' ' + val
					}
				}
			})
		</script>

相比watch,computed更适合做本地数据的变化 ,但是如果对变量做一些异步,网络请求等耗时的操作时watch是更为合适的。

                <div id="app">
			{{fullName}}
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el: '#app',
				data:{
					firstName:'Stepon',
					lastName:'Chou'
				},
				computed:{
					fullName(){
						return this.firstName + ' ' + this.lastName
					}
				}
			})
		</script>

推荐 Vue的计算属性(computed)和方法(methods)有什么区别

猜你喜欢

转载自blog.csdn.net/weixin_38289787/article/details/103501579