【问题标题】:Refresh component after router.navigateByUrl to the same URL with different parameters在 router.navigateByUrl 之后刷新组件到相同的 URL 不同的参数
【发布时间】:2019-08-17 23:12:18
【问题描述】:

我有一个路由器<router-outlet></router-outlet>,其中包含以下路由:

{ 路径:'employee/:id',组件:EmployeeComponent }

如果 EmployeeComponent 组件已经在带有特定员工的路由器插座中,我在我的代码中执行以下操作:this.router.navigateByUrl('/employee/'+this.employee2);

url改变了,但是组件没有执行新Employee的ngOnInit()方法,内容没有刷新。我需要用我的路由器上已有的 EmployeeComponent 实例化一个新的 EmployeeComponent,或者刷新现有的。怎么解决?

【问题讨论】:

  • IMO 你应该使用ActivatedRoute

标签: angular url router reload


【解决方案1】:

需要注入“ActivatedRoute”并订阅参数

constructor(route:ActivatedRoute){
      route.params.subscribe(val=>{ 
       //    add to do function()
      })
   }

【讨论】:

    【解决方案2】:

    您可以使用AcitvatedRoute

    import { ActivatedRoute } from "@angular/router";
    
    constructor(private route: ActivatedRoute) { }
    

    使用参数

    ngOnInit(){
      this.route.params.subscribe(param=>{
        console.log(+params['id'])
      })
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多