【问题标题】:Changing routes when loading child components angular4加载子组件时更改路线angular4
【发布时间】: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 模板就好了

标签: angular angular4-router


【解决方案1】:

将 this.router.navigate[('pathofdesiredcomponent')] 替换为 this.router.navigate(['pathofdesiredcomponent'])

【讨论】:

  • 好的,非常感谢路由器正在工作,如果我也想保持父组件的元素一致,该怎么办,谢谢
  • 我不确定你的意思。能举个例子吗?
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多