简体   繁体   English

Angular2 Router 中的两个组件可以抽象吗

[英]Can two components be abstract in Angular2 Router

I have three components:我有三个组成部分:

  1. Root component ( root ) that is a component bootstraps with app module.根组件 ( root ) 是带有 app 模块的组件引导程序。
  2. Toolbar component ( toolbar ) that contains toolbar for all it's children.工具栏组件( toolbar )包含所有子工具栏。
  3. Dashboard component ( dashboard ) that is first real page.仪表板组件( dashboard )是第一个真实页面。

So the root and toolbar should not have it's own URL, and dashboard should.所以roottoolbar不应该有自己的 URL, dashboard应该。 At the resulting HTML I expect that they are put into each other in the following way:在生成的 HTML 中,我希望它们以下列方式相互放入:

<root>
  <toolbar>
    ...some toolbar inner code
    <dashboard>
      ...dashboard page code
    </dashboard>
  </toolbar>
</root>

And all of this code should be loaded by single URL: http:///dashboard所有这些代码都应该通过单个 URL 加载:http:///dashboard

Now I have following code:现在我有以下代码:

root module routing root模块路由

const rootRoutes: Routes = [
  {path: '', component: ToolbarComponent},
];

toolbar module routing toolbar模块路由

const toolbarRoutes: Routes = [
  {path: 'dashboard', component: DashboardComponent},
];

It works, but replaces toolbar component with dashboard .它可以工作,但用dashboard替换toolbar组件。

How should I write my Routes arrays to fulfil this demand?我应该如何编写我的Routes数组来满足这个需求? With ui-router and deprecated component router on AngularJS I can do this.使用ui-router和 AngularJS 上已弃用的component router ,我可以做到这一点。

Add a router-outlet in ToolbarComponent 's templateToolbarComponent的模板中添加一个router-outlet

And your route config will be like你的路线配置会像

const rootRoutes: Routes = [
    { path: '',
      component: ToolbarComponent,
      children: {
        { path: 'dashboard', component: DashboardComponent }
      }
];

Now /dashboard will render both Toolbar and Dashboard现在/dashboard将同时呈现 Toolbar 和 Dashboard

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

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