简体   繁体   English

同级组件通信vuejs 2

[英]Sibling component communication vuejs 2

I have a Vue Component representing a page that looks like this (ParentComponent.vue): 我有一个Vue组件,表示一个看起来像这样的页面(ParentComponent.vue):

<template>
    <div id="main-wrapper" class="appTravelytics">
      <top-bar v-if="$route.path !== '/login'"></top-bar>
      <div class="page-wrapper" v-if="$route.path !== '/login'">
        <div class="container-fluid">
          <breadcrumb ></breadcrumb>
          <template>
            <router-view :loggedIn="loggedIn"></router-view>
          </template>
        </div>
        <footer-bar></footer-bar>
      </div>
      <template v-else>
        <router-view :loggedIn="loggedIn"></router-view>
      </template>
    </div>
</template>

BreadCrumb.vue: BreadCrumb.vue:

<template>
<h3 class="text-themecolor m-b-0 m-t-0">{{ title }}</h3>
</template>
<script>
    export default {
        data () {
          return {
            title: 'Welcome'
          }
        },
        created: function () {
          this.$root.$on('page_title', function (data) {
            console.log(data)
            this.title = data
            this.$nextTick()
          })
        }
      }
</script>

Content.vue (script portion): Content.vue(脚本部分):

export default {
      created: function () {
        this.$root.$emit('page_title', 'Page Title')
      }
    }

The component defined by the Router (in this example labeled Content.vue) will be placed in the router-view of ParentComponent, that same component needs to communicate with the "breadcrumb" component to update the title of the page and breadcrumbs. 路由器定义的组件(在此示例中标记为Content.vue)将放置在ParentComponent的路由器视图中,该组件需要与“面包屑”组件进行通信以更新页面和面包屑的标题。

I have tried a global event bus, I have tried prop passing. 我尝试了全局事件总线,尝试了道具传递。 you'll notice the console.log on receiving the emit. 您会在收到发射时注意到console.log。 It updates just fine, using VueJS debugger in chrome, the variable is populated. 使用chrome中的VueJS调试器,它可以很好地更新,并填充变量。 But I can not get it to update the DOM/presentation. 但是我无法获取它来更新DOM /表示。 I've searched all over and get the recommendations of prop passing and global bus, but something is missing. 我到处搜索,并获得有关道具传递和全球巴士的建议,但缺少一些东西。

Please help :) 请帮忙 :)

Thanks in advance! 提前致谢!

Vuex将更好地进行状态管理,以便您可以从所有组件访问和更新必要的状态。

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

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