【发布时间】:2018-01-12 23:34:53
【问题描述】:
我的父数据中有一个嵌套对象,它看起来像这样。
obj: {
1: {
'a': [ [], [], [] ]
'b': [ [] ]
},
2: {
'x': [ [], [] ]
}
}
在初始化时,在我父母的数据中,我将这个 obj 初始化为 {}。当我尝试在我的子组件中使用观察器时,它只观察 1、2 键级别,而不是在字母键级别发生更改时。但是,即使内部级别发生变化,我也希望它能够执行。
// in my component
computed: {
watchForms() {
alert("X");
}
},
然后,当新元素添加/删除到对象时,我尝试使用 emit/on 从根元素告诉子元素 - 发出事件并在组件上捕获它。
// 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 警告]:“更新”的事件处理程序出错:“TypeError:无法读取未定义的属性 'apply'”(在 中找到)
我收到此错误,但我没有打电话给任何.apply() :/ 我做错了什么?
【问题讨论】:
-
似乎
this.watchForms在created()中未定义为$on( ... )的参数 -
正确的做法是什么?不是每次 obj 发生一些变化都会触发它吗?
标签: javascript vuejs2 vue-component