0
我的父代数据中有一个嵌套对象,它看起来像这样。从父代中观察嵌套对象数据
obj: {
1: {
'a': [ [], [], [] ]
'b': [ [] ]
},
2: {
'x': [ [], [] ]
}
}
虽然初始化,在我父数据,我初始化这个obj
为{}
。当我尝试在我的子组件中使用观察器时,它仅监视1,2级密钥级别,而不是密钥级别发生更改时。但是,即使内层发生变化,我也希望它能够执行。
// in my component
computed: {
watchForms() {
alert("X");
}
},
然后,我试图用发射/从根元素告诉给儿童在新元素被添加/移除的对象 - 发射事件并捕获它的组分上。
// So I set a global variable,
Vue.prototype.globalEvents = new Vue();
// In my root element, initialised the obj
data: {
obj: {}
}
// In my root element methods, when changes happen on my obj
methods: {
changeHappened() {
this.globalEvents.$emit('updated');
}
}
// And in my component, I tried to catch it and run the method,
created: function() {
this.globalEvents.$on('updated', this.watchForms);
},
[Vue warn]: Error in event handler for "updated": "TypeError: Cannot read property 'apply' of undefined" (found in)
我收到此错误,但是我不叫任何.apply()
:/我在做什么错?
这似乎'this.watchForms'是未定义'()创建'作为参数传递给在'$(...)' – marekful
是什么做的正确方法吗?每次在obj中发生什么变化都不应该触发它吗? – senty