简体   繁体   English

兄弟组件通信在vue中不起作用

[英]Sibling component communication not working in vue

I am trying to send this.TC from typing.js to ending-page.js which are sibling components.我正在尝试将 this.TC 从 typing.js 发送到作为兄弟组件的 ending-page.js 。 Emits and event hubs not working.发射和事件中心不工作。 But emit from typing.js to parent works as I want.但是从 typing.js 到 parent 的发出可以按我的意愿工作。 (There will be only one more call in this app, so i don't want use Vuex if it isnt necessary for this - i want to do it with simple emits ) Here's my code: (这个应用程序中只会再调用一次,所以如果没有必要,我不想使用 Vuex - 我想用简单的发射来做)这是我的代码:

Parent:家长:

      <template>
    <div id = "app">

    <typing v-if = "DynamicComponent === 'typing'" />
    <ending_page v-else-if = "DynamicComponent === 'ending_page'" />

    </div>
  </template>
  <script>

  /* Importing siblings components to parent component */
  import typing from './components/typing/index.vue'
  import ending_page from './components/ending-page/index.vue'

  export default {


    name: 'app',
    components: {
      typing,
      ending_page
    },

    data() {
      return {

        DynamicComponent: "typing",

      };
    },

    methods: {

      updateDynamicComponent: function(evt, data){

        this.DynamicComponent = evt;

      },

    },


  };

  </script>

typing.js:打字.js:

      import { eventBus } from "../../main";

  export default {


    name: 'app',
    components: {

    },

    data() {
      return {

        /* Text what is in input. If you write this.input = "sometext" input text will change (It just works from JS to HTML and from HTML to JS) */
        input: "",

        /* Object of TypingCore.js */
        TC: "somedata",

        /* Timer obejct */
        timer: null,
        is_started: false,
        style_preferences: null,


      };
    },


      ICallThisFunctionWhenIWantToEmitSomething: function(evt) {

          /* Sending data to ending_page component */
          this.$root.$emit('eventname', 'somedata');

          /* Calling parent to ChangeDynamicComponent && sending TC.data what will be given to ending_page (I think it looks better with one syntax here) */
          this.$emit('myEvent', 'ending_page', this.TC.data);

        }

      },




  };

ending-page.js:结束-page.js:

      import { eventBus } from "../../main";

  export default {
    name: 'ending-page',
    components: {},

    data () {
      return {
        data: "nothing",
      }
    },

    computed: {

    },

    props: {

    },

    methods: {


    },

    /* I know arrow functions etc but i was trying everyting */
    created: function () {
      this.$root.$on('eventname', function (data) {
          console.log(data)
          this.title = data
          this.$nextTick()
      })
    }

  }

It is an example of how to share data between siblings components.这是如何在兄弟组件之间共享数据的示例。

Children components emits events to parent.子组件向父组件发出事件。 Parent components send data to children.父组件向子组件发送数据。

So, the parent has the property title shared between the children.因此,父母拥有孩子之间共享的财产title When typing emits the input event the directive v-model capture it an set the value on parent.typing发出input事件时,指令v-model将捕获它并在父项上设置值。

Ref:参考:

https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow https://v2.vuejs.org/v2/guide/components-props.html#One-Way-Data-Flow

https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components https://v2.vuejs.org/v2/guide/components.html#Using-v-model-on-Components

https://benjaminlistwon.com/blog/data-flow-in-vue-and-vuex/ https://benjaminlistwon.com/blog/data-flow-in-vue-and-vuex/

 Vue.component('typing', { props: { value: '' }, template: '<button @click="emit">Click to change</button>', methods: { emit() { this.$emit('input', `changed on ${Date.now()}`); } } }); Vue.component('ending-page', { props: { title: '' }, template: '<div>{{ title }}</div>', }); var app = new Vue({ el: '#app', data() { return { title: 'unchanged', }; }, });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <typing v-model="title"></typing> <ending-page :title="title"></ending-page> </div>

可以尝试使用 vuex 进行通信,您想要共享的数据在 this.$store.state 上进行,或者如果调用函数使用突变(同步函数)和操作(异步函数) https://vuex.vuejs.org/

I like what Jeffrey Way suggested once, just create a global events object (which accidentally can be another Vue instance) and then use that as an event bus for any global communication.我喜欢 Jeffrey Way 曾经提出的建议,只需创建一个全局事件对象(它可能是另一个 Vue 实例),然后将其用作任何全局通信的事件总线。

window.eventBus = new Vue();

// in components that emit:
eventBus.$emit('event', data);

// in components that listen
eventBus.$on('event');

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

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