vue.js - vue-router:使用命名 View 时将 Prop 传递到组件中

标签 vue.js vue-router

我正在使用 named views在我的 vue-router 里面。在这两个组件中,我需要获取 Prop 。

以下代码显示了路由的定义。第一个条目不起作用。在组件内部, Prop 将保持未定义状态。所以 Breadcrumb 和 Collection 组件没有获得任何属性

第二个示例有效,因为只有一个组件:

const routes = [
    {
        path: '/:cid',
        name: 'collection',
        props: true,
        components: {
            default: Collection,
            breadcrumb: Breadcrumb
        }
    },
    {
        path: '/:cid/:iid',
        name: 'item',
        props: true,
        component: Item
    },
]

我认为当只有一个组件时,props 属性才有效。

我没有在文档中找到解决方案,因此欢迎任何帮助。

最佳答案

是的,您可以使用多个组件并将 props 传递给每个组件:

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      components: {
        default: Home,
        foo: Foo
      },
      props: {
        default: { name: 'Home' },
        foo: { name: 'Foo' }
      }
    }
  ]
})

http://jsfiddle.net/jonataswalker/ykf0uv0d/

See related discussion.

关于vue.js - vue-router:使用命名 View 时将 Prop 传递到组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392730/

相关文章:

javascript - 检查使用vue-router时是否可以前进

vue.js - 为什么 beforeRouteLeave 从未被调用?

javascript - Vue.js 中的条件路由

javascript - vuejs - .bind(this) 没有按预期工作

javascript - Vue.js:为什么我的 v-else 指令导致我的元素被渲染多次?

vue.js - Vue/Nuxt 页面转换不起作用

vue.js - Vue2 使用 location.href 导航到外部 url

javascript - 如何像 React 中的 {...props} 一样解构 Vue 中的 props?

Vue.js 绑定(bind)到名称中带有点的 DOM 自定义事件(如引导事件)

javascript - 如何修复 "the requested module does not provide an export named ' default'"?