角度2:如何使用路由器传播HTML元素?

时间:2018-07-31 11:09:42

标签: javascript angular angular-routing router-outlet

有一个基本的DOM:

main
- child A        :goBack()
  -- subChild A  :*
- child B        :goBack()

我从app.component.html向所有DOM子级传播一个footer元素。 我想知道的是,为什么Back按钮没有出现在子子级A页面上?

请参见stackblitz

2 个答案:

答案 0 :(得分:1)

您必须将子代创建为实际的子代路线。否则,它将像其他路线一样处理,并完全替换<router-outlet>标签,在这种情况下为ChildComponent

const routes: Routes = [
  { path: '', component: MainComponent },
  {
    path: ':child', component: ChildComponent, children: [
      { path: 'subChildA', component: SubChildComponent }
    ]
  },
];

此外,您还需要在<router-outlet>模板中的第二个ChildComponent标签中,应该在该模板中呈现子路线(子孩子)。

看看修改后的stackblitz

答案 1 :(得分:0)

您需要为此创建子路由。检查此修改

https://stackblitz.com/edit/angular-oupmd9

特别是 app-routing.module.ts