【问题标题】:How to get component name or the python from vue-router?如何从 vue-router 获取组件名称或 python?
【发布时间】:2018-08-01 17:44:59
【问题描述】:

我用vue-router之类的:

export default new Router({
routes: [
{
  path: '/',
  components: {
    navbar: Navbar,
    subnavbar: SubNavbar,
    main: WhoisPage   //********
  }
}

在我的组件 WhoisPage.vue 中,我想知道组件路径或主模块(即 //****** 行)使用哪个组件?

vue 或者 vuex 怎么做?

【问题讨论】:

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


    【解决方案1】:

    首先,vue-router 上没有“components”属性。

    您需要使用“component”属性来注入您的 shell 组件(在这种情况下,它看起来像 WhoisPage),然后您可以调用您的子组件在您的 shell 组件中构建您的页面。

    如果您想根据路由自动更改子组件,您可以将它们的名称作为字符串发送并在您的 shell 组件中呈现。

    // in your router config.
    
    export default new Router({
      routes: [
        {
          path: '/',
          component: WhoisPage,
          meta: {
            navbar: 'NavbarComponent'
          }
        }
      ]
    })
    <!-- in your shell component, ofc your navbar needs to be imported -->
    
    <component :is="$route.meta.navbar"></component>

    不确定我是否正确回答了您的需求。

    【讨论】:

      猜你喜欢
      • 2017-05-21
      • 1970-01-01
      • 2022-01-17
      • 2018-11-08
      • 2017-08-13
      • 1970-01-01
      • 2014-07-06
      • 2014-03-17
      相关资源
      最近更新 更多