【问题标题】:vue-router: nested named views problem. Additional component is not visiblevue-router:嵌套命名视图问题。附加组件不可见
【发布时间】:2019-01-05 23:16:23
【问题描述】:

我在 vue-router 文档中找到了这个例子:

https://jsfiddle.net/posva/22wgksa3/?fbclid=IwAR2WITnr6cmA_3rUgOCCvPrHASKfSW9QsvEKv4HrxBmuDUN1VmWSWufCtAI

const router = new VueRouter({
   mode: 'history',
   routes: [{ 
       path: '/settings',
       // You could also have named views at tho top
       component: UserSettings,
       children: [{
           path: 'emails',
           component: UserEmailsSubscriptions
           }, {
           path: 'profile',
           components: {
               default: UserProfile,
               helper: UserProfilePreview
           }
       }]
   }]
})

我试图做这样的事情:

https://jsfiddle.net/pt9o6h8e/

const router = new VueRouter({
   mode: 'history',
   routes: [{ 
       path: '/settings',
       components: {
           default: UserSettings,
           test: TestComponent
       }
       children: [{
           path: 'emails',
           component: UserEmailsSubscriptions
           }, {
           path: 'profile',
           components: {
               default: UserProfile,
               helper: UserProfilePreview
           }
       }]
   }]
})

但 TestComponent 不可见。 为什么?

【问题讨论】:

    标签: javascript vue.js vue-router


    【解决方案1】:

    当你这样指定你的路线时,

    { 
      path: '/settings',
      // You could also have named views at tho top
      components: {
        default: UserSettings,
        test: TestComponent
      }
    }
    

    UserSettingsTestComponent 被视为同级组件,因此在渲染时,它将在您在最顶层 div 上指定的默认 router-view 旁边查找名为 router-view#app

    但是,你把<router-view name="test" /> 里面 UserSettings 模板,所以它没有找到 router-view 来渲染 TestComponent 到。

    要解决此问题,请将test 路由器视图从UserSettings 移动到#app

    <div id="app">
      <h1>Nested Named Views</h1>
      <router-view></router-view>
      <router-view name="test" class="us__content us__content--helper"/>
    </div>
    

    或者,如果您想将其保留在 UserSettings 中,请通过将 test: TestComponent 移动到您的任一子路由中来更改声明它的方式。

    working example

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 2016-06-03
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多