【问题标题】:How do you pass data between VueJS components when using the Vue router?使用 Vue 路由器时,如何在 VueJS 组件之间传递数据?
【发布时间】:2017-08-28 17:09:46
【问题描述】:

我有这些路线设置:

Vue.use(Router)
export default new Router({
  routes: [
    {
      path: '/group/:id',
      name: 'Group',
      component: GroupContainer,
      children: [
        {
          // when /group/:id/posts is matched
          path: 'posts',
          component: ViewPosts
        },
        {
          // when /group/:id/topics is matched
          path: 'topics',
          component: ViewTopics
        },
        {
          // when /group/:id/people is matched
          path: 'people',
          component: ViewPeople
        }
      ]
    }
  ]
})

所以组组件在这里有一个带有路由器视图的模板

<template>
  <div class="group">
    I am a group page
    <router-view></router-view>
  </div>
</template>

因此,组组件最终成为 ViewPosts、ViewTopics 或 ViewPeople 的父级,但它并不直接包含这些组件。

将数据从父级(组)传递给子级(ViewPosts...)并允许子级向父级发出数据的好系统是什么?

【问题讨论】:

    标签: vue.js vuejs2 vue-component vue-router


    【解决方案1】:

    一种方法是将属性和处理程序添加到router-view

    <div class="group">
      I am a group page
      <router-view :topics="topics"
               :people="people"
               :posts="posts"
               @topic-emitted="onTopicEmitted">
      </router-view>
    </div>
    

    这是一个快速的example

    【讨论】:

    • @ben You;re welcome :) 我通常不会这样做,但这是你的第一个问题;如果答案帮助您/回答了您的问题,您通常应该投票或接受答案,或两者兼而有之。不过完全取决于您。
    猜你喜欢
    • 2016-09-19
    • 1970-01-01
    • 2020-02-12
    • 2020-06-12
    • 1970-01-01
    • 2019-07-08
    • 2021-09-12
    • 2020-08-11
    • 1970-01-01
    相关资源
    最近更新 更多