Vuejs $ emit在callback中不会触发

在下面的代码中:

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' }); } }) } } } 

当调用remove函数时,控制台会logging“Should Fire”,但是$ emit事件不会被触发。 如果我将$ emit放在callback之外,如下所示:

 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事件不能在callback中触发?

所以事实certificate这是在NoteRepo造成的问题。 具体来说,Firebase事件callback。

 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); } 

我不确定到底是什么错误,但现在看起来好像是这样。