如题,在组件中监听外层传入的prop时,调用的this.data中包含了我需要的属性 list 和已经在 attached 阶段完成初始化的数据,但直接调用 this.data.list 时返回的却是空数组,而不是this.data展开后看到的list属性的内容,这让我无法理解,相关代码如下:
<!--components/comp-a/index.wxml-->
<view class="item" wx:key="index" wx:for="{{array}}">
{{ item }}
</view>
<!--index.wxml-->
<view class="index">
<comp-a name="abc" />
</view>
// components/comp-a/index.js
Component({
/**
* 组件的属性列表
*/
properties: {
name: String,
},
/**
* 数据监听
*/
observers: {
name: function (val) {
console.log("comp-a observers name val", val);
console.log("comp-a observers name data", this.data);
console.log("comp-a observers name list", this.data.list);
},
},
/**
* 生命周期
*/
lifetimes: {
/**
* 初始化数据
*/
attached: function () {
this.setData({
list: [1, 2, 3, 4, 5],
});
},
},
/**
* 组件的初始数据
*/
data: {
list: [],
},
});
对象外层加上JSON.stringify试下 console.log("comp-a observers name data", JSON.stringify(this.data)); console.log("comp-a observers name list", JSON.stringify(this.data.list));