Vue components communication

前端 未结 6 1391
臣服心动
臣服心动 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:36

    In addition to pesla' answer take a look at the guide's State Management section under Building large scale apps: http://vuejs.org/guide/application.html#State_Management . I've created a jsfiddle based on that here: https://jsfiddle.net/WarwickGrigg/xmpLg92c/.

    This technique works for components too: parent-child, sibling-sibling component relationships etc.

    var hub = {
      state: {
        message: 'Hello!'
      }
    }
    
    var vmA = new Vue({
        el: '#appA',
        data: {
          pState: {
            dA: "hello A" 
        },
        hubState: hub.state
      }
    })
    
    var vmB = new Vue({
        el: '#appB',
        data: {
          pState: {
            dB: "hello B"
        },
        hubState: hub.state
      }
    })
    

提交回复
热议问题