简体   繁体   English

通过路由器传递道具不起作用

[英]Passing props via router doesn't work

I'm using VueJS 2.3.3 and coffescript and I'm trying to pass a prop to a component from the router, but I'm having no success. 我正在使用VueJS 2.3.3和coffescript,并且试图将prop从路由器传递到组件,但是没有成功。 Code is not mine, so I'm having some trouble figuring out what am I doing wrong. 代码不是我的,所以我在弄清楚我在做什么错时遇到了麻烦。 Here's my router: 这是我的路由器:

App = require './views/App'

Shared = {
  header: require('./views/shared/header'),
  global_loader: require('./views/shared/global_loader.vue')
}

view = (view_name) ->
  require("./views/#{view_name}")

componentize = (view_name, options = {}) ->
  options.include_header ?= true

  component = options.component || {}

  component.app ?= view(view_name)
  component.header = Shared.header if options.include_header

  component

exports.routes = [
  {
    path: '/',
    component: App
    children: [
      {
        path: '/component',
        components: componentize('path/to/component'),
        props: { has_groups: true }
      },
      ...
    ]
  }
  ...
}

Here's my App.vue code: 这是我的App.vue代码:

  <template lang="pug">
    #app-wrapper
      transition(name="fade")
        router-view(name="global_loader")

      #header-wrapper
        router-view(name="header")

      #current-view-wrapper.container
        transition(name="enter")
          router-view(name="app")
    </template>

On my component, I'm receiving the prop as usual: 在组件上,我照常收到道具:

props:
   has_groups:
      default: false

Everything works fine, except that has_groups doesn't receive the correct prop value from the router. 一切正常,除了has_groups没有从路由器接收正确的prop值。 It doesn't change to true. 它不会变为true。

Can anyone help me finding out what I'm missing? 谁能帮我找出我所缺少的吗?

I found the solution. 我找到了解决方案。 As I'm using named routes, I have to configure the props like: 当我使用命名路线时,我必须像下面这样配置道具:

props:
  global_loader: false
  header: false
  app: (route) -> ({ has_groups: true })

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

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