uniapp中computed踩得坑

博客围绕JavaScript代码展开,展示了不同方式定义对象属性后`console.log`的输出情况,如`this.list = {}`和`this.list = {order:[], counts: 0}`等,推测输出差异与重新定义对象属性的方式有关,寻求大佬解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uniapp中computed踩得坑

<view class="title">{{marketLength}}</view>
<button @click="add">加长度</button>
<button @click="clear">清空</button>

data() {
	return {
		list: {
			order: [],
			counts: 0
		}
	}
},
computed:{
	marketLength(){
		let counts = 0
		for (let h in this.list.order) {
			counts +=  Number(this.list.order[h].count)
		}
		return counts
	}
},
onLoad(){
	console.log('onLoad===',this.list.order)
},
methods: {
	add(){
		this.list.order.push({count:1})
	},
	clear(){
		this.list = {
			order:[],
			counts: 0
		}
		console.log('clear===',this.list.order)
		/** 以下清空后不会走computed **/
		// 好像是动态添加属性后没有get和set方法
		// 正在找原因
		// this.list = {}
		// this.list.order = []
		// this.list.counts = 0
		// console.log('clear===',this.list.order)  输出
	}
}

onLoad(){
console.log(‘onLoad===’,this.list.order)输出为下图
}
在这里插入图片描述

this.list = {}
this.list.order = []
this.list.counts = 0
console.log(‘clear===’,this.list.order) 输出为下图
在这里插入图片描述
this.list = {
order:[],
counts: 0
}
console.log(‘clear===’,this.list.order)输出为下图
在这里插入图片描述
目前只发现这些区别,应该和重新定义对象的属性方式有关。
有大佬知道的话麻烦告知下,感谢

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值