为什么vue监听到对象数据,但是对象内的值取不到?-灵析社区

希望奇迹发生_1

console.log(allDict);这句打印出来有值,为什么里面的值是undefined? allDict: { handler(allDict) { console.log(allDict); console.log(allDict.AnimalType); Object.keys(allDict).forEach(key => console.log(key, allDict[key])); }, deep: true,// 是否开启深度监听 immediate: true// 是否初始化时就执行一次 }, ![image.png](https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241024/37beed1ab00399591457aed9633e2142.png)

阅读量:41

点赞量:0

问AI
父组件: /* 查询所有的数据字典 */ async getAllDict(){ this.animalTypeData = await this.getDictionaryByCode("AnimalType"); this.$set(this.allDict, "AnimalType", this.animalTypeData); }, 子组件watch: /* 数据字典 */ allDict: { handler(allDict) { console.log(allDict); console.log(allDict.AnimalType); Object.keys(allDict).forEach(key => console.log(key, allDict[key])); this.animalTypeData = allDict.AnimalType; }, deep: true,// 是否开启深度监听 immediate: true// 是否初始化时就执行一次 }, 上面用这种this.$set(this.allDict, "AnimalType", this.animalTypeData);赋值方式试了没问题,后面没采用watch的方式去监听allDict,直接用了计算属性也能拿到 // 类型数据 computed: { animalTypeData() { return this.allDict.AnimalType; } }