在下面的代码中:
export default {
props: ['note'],
methods: {
remove(){
NoteRepo.remove(this.note, (err) => {
if (err) {
console.log('Should Fire')
this.$emit('alerted', {
type: 'error',
message: 'Failed to remove note'
});
}
})
}
}
}
当调用删除函数时,控制台会记录“Should Fire”,但 $emit 事件不会被触发。如果我将 $emit 移到回调之外,如下所示:
export default {
props: ['note'],
methods: {
remove(){
this.$emit('alerted', {
type: 'error',
message: 'Failed to remove note'
});
NoteRepo.remove(this.note, (err) => {
if (err) {
console.log('Should Fire')
}
})
}
}
}
有用。我尝试过分配_this = this
并用它来触发 $emit 但没有区别。
为什么 $emit 事件不会在回调中触发?
所以事实证明是 NoteRepo 中的某些内容导致了问题。特别是 firebase 事件回调。
constructor () {
super();
// Initialize Firebase
Firebase.initializeApp({
apiKey: "xxx",
authDomain: "xxx",
databaseURL: "xxx",
storageBucket: "xxx",
messagingSenderId: "xxx"
});
this.ref = Firebase.database().ref('notes');
this.attachFirebaseListeners();
}
attachFirebaseListeners() {
this.ref.on('child_added', this.onAdded, this);
this.ref.on('child_removed', this.onRemoved); // Removing 'this' here seems to of fixed it.
this.ref.on('child_changed', this.onChanged, this);
}
我不确定到底出了什么问题,但现在似乎已经解决了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)