计算属性与监听器

1、watch用法

watch监听data中属性的改变,与data和methods平级
以下展示两种方法书写watch中的内容

//方法一
firstName:function(val){
	 console.log(val);
	 this.name=this.firstName+'-'+this.lastName
	 },
	 lastName:function(val){
	 console.log(val);
	 this.name=this.firstName+'-'+this.lastName
	 },
//方法二
firstName: {
	 handler(newVal, oldVal) {
	 console.log(newVal)
	 console.log(oldVal)
	 },
		immediate: true,//立即执行
		deep: true,//深度监听
	 },
2、computed用法

与data和methods平级
特点:在computed中计算的属性,在data中不用再定义

//方法一
name(){
	return this.firstName+'-'+this.lastName
	}
//方法二
name: {
	get() {
		return this.firstName + '-' + this.lastName
	},
	set(val) {
		console.log(val.split('-'))
		this.firstName = val.split('-')[0]
		this.lastName = val.split('-')[1]
	},
},
3、method,watch,computed的区别

1.computed主要当做属性来使用,使用时不加()
2.methods方法表示一个具体的操作,主要书写业务逻辑
3.watch监听data中数据,data中数据变化时则执行watch中函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值