Vue components communication

前端 未结 6 1380
臣服心动
臣服心动 2020-12-01 09:18

I have two Vue components:

Vue.component(\'A\', {});

Vue.component(\'B\', {});

How can I access component A from component B? How does the

6条回答
  •  庸人自扰
    2020-12-01 09:42

    Communicate between two Vuejs components has many options. If your components are parent and child then you should use "props" to send data from parent to child and use "events" to communicate from child to parent. If your components are sibling then you need to use "store" else you can use "$root" property.

    Parent to child

    parent component

    
    

    child component must have property

    props: ['propsData']
    

    Child to Parent

    child component

    this.$emit('messegeToParent', arg1, arg2, ...);
    

    parent component

    
    

    The below method should be in child component

    messageReceivedFromChild(arg1, arg2, ...) {
    
    }
    

    Sibling components

    component 1

    this.$root.$emit('message1', arg1, arg2, ...);
    

    component 2

    this.$root.$on('message1', (arg1, arg2, ...) => {
    
    });
    

提交回复
热议问题