Angular2 Component 不会为参数化路由重新初始化

标签 angular typescript angular2-routing

我有以下带有参数 T1 的测试路线:

{
    path: 'Test/:T1',
    component: TestComponent
},

当我从“Test/1”路由到“Test/2”时,我的 TestComponent 没有重新初始化。这是 Angular 路由器的问题吗?

我正在使用 "@angular/router": "3.0.0-beta.1"

最佳答案

这是目前唯一支持的行为。有计划使其可配置 https://github.com/angular/angular/issues/9811

您可以订阅参数更改并在那里进行初始化

export class MyComponent {
    constructor(private route : ActivatedRoute,
      private r : Router) {}

    reloadWithNewId(id:number) {
        this.r.navigateByUrl('my/' + id + '/view');
    }

    ngOnInit() {
      this.sub = this.route.params.subscribe(params => {
         this.paramsChanged(params['id']);
       });
    }

    paramsChanged(id) {
      console.log(id);
      // do stuff with id

    }
}

另见 How do I re-render a component manually?

关于Angular2 Component 不会为参数化路由重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847642/

相关文章:

angular - .NET Core 2.1 SignalR 使用 Angular 6 客户端进行长轮询的回退

angular - 使用导入模块中的组件

css - 如何将 flag-icon-css 与 angular2/4 一起使用?

javascript - 如何使用 Jest 在 typescript 中模拟 Date 对象?

javascript - 将我的站点配置注入(inject)到 Angular 8 的其他模块中

javascript - 多个值的 Angular 过滤器对象数组

Angular2 获取路由映射

html - 如何在 angular2 中使用一个 routerLink 触发多个导出(2.1.1)

angular - 在 Angular 2 测试中 stub ActivatedRoute

angular - 打开新窗口 - Electron