【发布时间】:2017-11-27 16:29:48
【问题描述】:
我的父组件 html 有一些元素我需要在整个过程中保持不变但我的子组件会随着路由一起更改,因此子组件加载正常但路由的更改没有得到反映 [如果我尝试点击路由路径直接更改地址栏,它会被渲染但没有父组件html元素存在]
父组件
`<parent-component>
<child1-component></child1-component>
<child2-component></child2-component>
<child3-component></child3-component>
//button for loading of child components which would be consistent
throughout in parent component along with child components being loaded
<button (click)="func()"></button>
</parent-component>`
父路由模块
`const routes: Routes = [
{ path: 'parentcomponent', component: parentComponent },
{ path: 'child1component', component: child1Component },
{ path: 'child2component', component: child2component },
{ path: 'child3component', component: child3component },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
并且在我的子组件中成功执行我的功能
子组件
`this.router.navigate[('pathofdesiredcomponent')]`
我尝试了普通路线和子路线,但它不起作用
【问题讨论】:
-
你在哪里使用
? -
@mohituprim 截至目前 i
仅存在于 app.component.html 中,当我使用子路由时,我也在父组件中给出了 -
你定义你的主路由像@NgModule({ imports: [ RouterModule.forRoot(routes)], exports: [RouterModule], declarations: [] }) export class AppRoutingModule { }
-
是的,这样,我可以给一个要点链接或其他东西,以便你可以帮助我
-
是的,如果你能创建 plunkr 模板就好了