2017-03-31 109 views
1

如何获得在VUE路由器的下一个路由如何生成基于一个基本路径子组件路由

我有以下途径:/principal

{path: '/principal', component: Principal}

现在,我需要开车具有相同的URL基地等组成, 新网址如下:

/principal/compa

是否有可能让单个基本路线能够显示其他组件?

类似这样的事情(我知道vue-router不能像这样工作),但你如何得到这种行为?

{ path: '/principal', component: Principal, subpath: { path: 'compa', component: 'CompA' } }

感谢

回答

1

有一个在VueRouter构造配置一个children选项来渲染嵌套路线Vue公司的组件。

在这种特殊情况下,这将是:

{ 
    path: '/principal', 
    component: Principal, 
    children: [{ 
    path: 'compa', // it would match /principal/compa 
    component: CompA 
    }] 
} 

从VUE路由器DOC:

const router = new VueRouter({ 
    routes: [ 
    { 
     path: '/user/:id', 
     component: User, 
     children: [ // <-- notice the children property 
     { 
      // UserProfile will be rendered inside User's <router-view> 
      // when /user/:id/profile is matched 
      path: 'profile', 
      component: UserProfile 
     }, 
     { 
      // UserPosts will be rendered inside User's <router-view> 
      // when /user/:id/posts is matched 
      path: 'posts', 
      component: UserPosts 
     } 
     ] 
    } 
    ] 
}); 

看一看nested routes了解更多详情。

+0

嗨,我理解行为,但如果父组件具有我不想显示的内容?,我只需要显示子组件的内容。 因为你建议的方式,我在同一时间显示父亲和儿子的内容 – chalo

+0

@chalo这一切都取决于组件的结构。例如,您可以使用一个简单的组件,其模板中仅包含一个''作为基础路线,然后使用'children'中的空路径来设置基础路线的组件。在这种情况下,其他嵌套路由不会与基本路由(由空路径指定)共享相同的视图。 –