【发布时间】:2017-06-28 16:45:35
【问题描述】:
我在一个名为 app.js 的文件中创建了一个名为 EvenBus 的全局 vuejs 事件,如下所示:
window.EventBus = new Vue();
从数据库中删除一个人后,我重定向到 home 页面并发出一个名为 deleted 的事件,如下所示:
import {app} from '../app';
export default {
methods: {
deleteMe: function () {
axios.delete('/api/persons/' + this.person.id)
.then(response => {
window.location.href = '/home';
EventBus.$emit('deleted', {
notification_msg: 'This person has been successfully deleted.',
});
})
},
}
在主页上,我插入了一个标签<notify></notify>,链接到一个名为notify.vue 的组件。
在这个组件中,我添加了以下脚本:
import {app} from '../app';
export default {
/* ... */
mounted() {
let that = this;
console.log(EventBus);
EventBus.$on('deleted', function(data){
alert('person deleted!!' + data.notification_msg);
that.msg_text = data.notification_msg;
});
},
}
当删除发生时,我被成功重定向到home 页面,但那里没有任何反应。 alert('person deleted!!...') 永远不会出现。
代码执行没有错误。
我是否遗漏了一些东西来让我的组件监听发出的事件?
编辑
notify.vue 文件中写入的行 console.log(EvenBus); 显示有一个名为“已删除”的事件(参见下面的打印屏幕)
【问题讨论】:
-
重定向到
/home会导致页面加载是吗? -
是的,它作为 http 请求加载。它不是使用 ajax 或任何东西完成的。仅限
window.location.href = '/home'; -
所以......你发出的事件基本上已经消失了,因为页面已经消失了,如果它甚至发出它的话。
-
好的,我明白了。请添加您的评论作为答案,以便我可以根据需要将 if 作为答案。
标签: vue.js vuejs2 vue-component