简体   繁体   English

Vue.js eventBus 在页面刷新后不发出属性

[英]Vue.js eventBus doesn't emit property after page refresh

I want to pass property from one component to other, they are siblings, after a page refresh.我想在页面刷新后将属性从一个组件传递给另一个组件,它们是兄弟姐妹。 I want to do this on the page load.我想在页面加载时执行此操作。 I am using eventBus to do it:我正在使用 eventBus 来做到这一点:

EventBus.$emit('generatedSum', this.sum);

Receiving it in the sibling component with:在兄弟组件中接收它:

EventBus.$on('generatedSum', (sum) => {
      this.correct = sum;
    });

and I am puting both in the lifecycle hook:我将两者都放在生命周期挂钩中:

created(){}

The problem is that after code is compiled after a saved change, I get the property emited to the sibling component, but when I manualy refresh the page the property is not visible in the sibling component.问题是,在保存更改后编译代码后,我将属性发送到同级组件,但是当我手动刷新页面时,该属性在同级组件中不可见。

I think when the page refreshes the Vue components mount one after another.我认为当页面刷新时,Vue 组件会一个接一个地挂载。 Since both are siblings, when the first component trigger the event the second component may not be available to listen to the triggered event .由于两者都是兄弟,当第一个组件trigger the event时,第二个组件可能无法监听triggered event Because the second component is mounted after the first component.因为第二个组件是在第一个组件之后安装的。

As a work around try mounted() rather than using created() life cycle event.作为一种解决方法,请尝试mounted()而不是使用created()生命周期事件。

More on life cycle events can be found here.可以在此处找到有关生命周期事件的更多信息。 https://v2.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram https://v2.vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM