uni-app icon indicating copy to clipboard operation
uni-app copied to clipboard

v2小程序端给对象设置动态key,如果key中包含[]会导致setData失败,js报错

Open SepVeneto opened this issue 6 months ago • 3 comments
trafficstars

发行方式

小程序

具体平台

微信开发工具

开发环境

Windows

项目创建方式

CLI命令行

依赖版本

2.0.2-4060520250512001

问题描述

data: {
    test: {},
}
onLoad: {
    this.$set(this.test, '[key]', 1)
}

v2在执行diff的时候会将对象中变更的key扁平化,当key中包含[]时diff的结果会变成

this.setData({
    test.[key]: 1
})

导致微信小程序在执行的时候报错

data path descriptor "test.[key]" is illegal at char 6 (illegal index)

重现步骤

  1. 新建uni-app v2项目
data: {
    test: {},
}
onLoad: {
    this.$set(this.test, '[key]', 1)
}

期望行为

No response

实际行为

No response

截图或录屏

No response

SepVeneto avatar May 15 '25 06:05 SepVeneto

你的问题是 this.$set(this.test, '[key]', 1) 这里你设置的属性是 [key] ,uniapp 编译出来为 [test] 吗?我看你写的是 this.setData({ test.[test]: 1 })

chouchouji avatar May 15 '25 06:05 chouchouji

你的问题是 this.$set(this.test, '[key]', 1) 这里你设置的属性是 [key] ,uniapp 编译出来为 [test] 吗?我看你写的是 this.setData({ test.[test]: 1 })

不好意思,复制错了,但是意思是一样的,就是diff的结果会把对象排平,导致key包含了中括号

https://github.com/dcloudio/uni-app/blob/v2.0.2-4060520250512001/packages/uni-mp-vue/dist/vue.runtime.esm.js#L4443

SepVeneto avatar May 17 '25 06:05 SepVeneto

你的问题是 this.$set(this.test, '[key]', 1) 这里你设置的属性是 [key] ,uniapp 编译出来为 [test] 吗?我看你写的是 this.setData({ test.[test]: 1 })

不好意思,复制错了,但是意思是一样的,就是diff的结果会把对象排平,导致key包含了中括号

https://github.com/dcloudio/uni-app/blob/v2.0.2-4060520250512001/packages/uni-mp-vue/dist/vue.runtime.esm.js#L4443

了解你的意思了,感谢反馈,目前先避免这样的参数作为对象的key吧

chouchouji avatar May 17 '25 07:05 chouchouji