简体   繁体   English

Vue.js中的Eventbus没有更新我的组件

[英]Eventbus in Vue.js isn't updating my component

I have two components and I want to display what the user enters in one on the other component. 我有两个组件,我想显示用户在另一个组件中输入的内容。 I don't really want to use a state manager like vuex because it's probably a bit overkill as it's a small application 我真的不想使用像vuex这样的状态管理器,因为它可能有点矫枉过正,因为它是一个小应用程序

this is my main.js: 这是我的main.js:

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
import { routes }from './routes';

export const EventBus = new Vue();

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
  mode: 'history'
});

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

Component that emits the event called addHtml.vue 发出名为addHtml.vue的事件的组件

<template>
    <div>
        <h1>Add HTML</h1>
        <hr>
        <button @click="navigateToHome" class="btn btn-primary">Go to Library</button>
        <hr>
        Title <input type="text" v-model="title">
        <button @click="emitGlobalClickEvent()">Press me</button>
    </div>

</template>

<script>
  import { EventBus } from '../../main.js'

  export default {
    data: function () {
      return {
        title: ''
      }
    },
    methods: {
      navigateToHome() {
        this.$router.push('/');
      },
      emitGlobalClickEvent() {
        console.log(this.title);
        EventBus.$emit('titleChanged', this.title);
      }
    }
  }
</script>

the file that listens for the event thats emitted and to display what was entered on the other component: 侦听发出的事件并显示在其他组件上输入的内容的文件:

<template>
    <div>
        <h1>Existing Items</h1>
        <hr>
        <p>{{ test }}</p>
    </div>
</template>

<script>
    import { EventBus } from '../main.js';

    export default {
      data: function () {
        return {
          test: ''
        }
      },
      created() {
        EventBus.$on('titleChanged', (data) => {
          console.log('in here!',data);
          this.test = data;
        });
      }
    }

</script>

the console.log('in here!',data); console.log('in here!',data); inside the listener gets printed out to the console so I know it's picking it up however {{ test }} doesn't get updated to what the user enters when I click back onto the component to view if it was updated, it just remains blank? 在侦听器内部打印到控制台,所以我知道它正在捡起它但{{ test }}没有更新到用户输入的内容当我点击返回到组件以查看它是否已更新时,它只是保持空白? Any Ideas? 有任何想法吗?

If you are using vue-router to display the secound component. 如果您使用vue-router显示secound组件。 The reason might be that you just see a new instance of that component everytime and the value of test will be reseted when the component is destroyed. 原因可能是您每次只看到该组件的新实例,并且在销毁组件时将重置test的值。 You can bind it to a (global) variable to persist test: window.yourApp.test . 您可以将其绑定到(全局)变量以持久test: window.yourApp.test Maybe webpack will mourn but it is possible. 也许webpack会哀悼但是有可能。 Even eslint has an ignore comment for such cases. 即使是对于这种情况,eslint也会忽略评论。

It is like Reiner said, because the component gets destroyed once you switch pages. 就像Reiner说的那样,因为一旦切换页面,组件就会被破坏。 Try wrapping your router-view inside a keep-alive: 尝试将路由器视图包装在keep-alive中:

<keep-alive>
  <router-view><router-view>
</keep-alive> 

Also. 也。 If you want to keep the state of just one specific component / page you can use the include tag: 如果要保留一个特定组件/页面的状态,可以使用include标记:

<keep-alive include='name of component'>
  <router-view></router-view>
</keep-alive>

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

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