简体   繁体   English

带有vue-router的Vue.js中的兄弟组件?

[英]Sibling components in Vue.js with vue-router?

I have a few of components mapped to several different routes, and I am wondering if the components created for each route are considered sibling components in terms of how data is passed around? 我有一些组件映射到几个不同的路由,并且我想知道为每个路由创建的组件在数据传递方式方面是否被视为同级组件? It is unclear which component is the parent component in this structure: 目前尚不清楚哪个组件是此结构中的父组件:

PageOne = Vue.extend( {
   template: "#page-one"
})
PageTwo = Vue.extend({
   template: "#page-two"
})
Home = Vue.extend({
  template: "#home"
})
var router = new VueRouter()
router.map({
  '/': {
    component: Home
  },
 '/PageOne': {
   component: PageOne
  },
'/PageTwo': {
  component: PageTwo
 }
})


var App = Vue.extend({})
router.start(App, "#app")

So if I want to pass data from the Home route to PageOne , would I need to use a global event bus, or could I use props to pass the data from one route to the next? 因此,如果要将数据从Home路由传递到PageOne ,是否需要使用全局事件总线,还是可以使用props将数据从一个路由传递到下一个路由? Here is a demo: http://codepen.io/p-adams/pen/kXwxRR 这是一个演示: http : //codepen.io/p-adams/pen/kXwxRR

You should either use a global event bus or state management with Vuex, OR set these up as nested routes ( http://router.vuejs.org/en/nested.html ): 您应该将全局事件总线或状态管理与Vuex一起使用,或者将它们设置为嵌套路由( http://router.vuejs.org/en/nested.html ):

router.map({
  '/': {
    component: Home,
    subRoutes: {
      '/PageOne': {
        component: PageOne
      },
      '/PageTwo': {
        component: PageTwo
      }
    }
  }
}) 

If you do it that way, you can pass props to your <router-view> : 如果这样做,则可以将道具传递到<router-view>

<div id="app">
  <router-view :foo="bar"></router-view>
</div>

http://router.vuejs.org/en/view.html http://router.vuejs.org/en/view.html

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

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