问题
在微信小程序中,如果传递给组件的参数字段名是“id”,组件内获取this.data.id并非真正传递值。
触发条件
微信开发者工具版本:1.05.2108202
基础库版本:2.19.2
<testComponent id="{{4}}" info="{{4}}" />
<testComponent id="{{5}}" info="{{5}}" />
<testComponent id="{{6}}" info="{{6}}" />
// testComponent.js
Component({
properties: {
id: {
type: Number,
value: 0,
},
info: {
type: Number,
value: 0,
},
},
lifetimes:{
attached() {
console.log(this.id, this.data.id, this.data.info, this);
},
},
});
this.id打印出来是顺序的,从1开始算,字符串类型。猜测是类似key的关键字作为索引。
this.data.id打印出来的都是默认值0(不解,按理说properties里面的参数会跟this.data合并)。
this.data.info仅仅是换了字段名(不叫id),能正常打印出传值。
总结
组件里面,字段名尽量不要取"id",以免出现冲突。