【发布时间】:2019-05-24 11:55:46
【问题描述】:
我不明白为什么我们在 Angular 中需要子组件?
因为我们可以使用选择器,并且可以获取其他组件的视图。 那么我们什么时候需要在 app.module 的路由定义中定义子节点?
上面代码和下面代码在定义children和导航方面有什么区别?
parent-component.html
</div>
<nested></nested>
</div>
child-component.ts
@Component({
selector: 'nested',
templateUrl: './nested-component.component.html',
styleUrls: ['./nested-component-list.component.css']
})
如果我们可以在 app.module 上没有任何定义的情况下实现这一点(假设 2 个组件在同一个角度模块中 - 我有 app.module),那么使用 chidren 和定义子路由的目的是什么?
在 app.module 中
path: 'parent-component', //<---- parent component declared here
component: ParentComponent,
children: [
{
path: 'child-one',
component: ChildComponent
}
........
并在 **父组件.html*
<div>
<router-outlet></router-outlet>
</div>
在
**parent-component.ts**
this.m_Router.navigate(["/child-one"]
【问题讨论】: