Angular2组件不会针对参数化路线重新初始化 [英] Angular2 Component does not reinitialize for parameterized route

查看:74
本文介绍了Angular2组件不会针对参数化路线重新初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的测试路线中使用了参数T1:

I have below test route with parameter T1:

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

当我从'Test/1'路由到'Test/2'时,我的TestComponent不会重新初始化.角度路由器有问题吗?

When I route to 'Test/2' from 'Test/1' my TestComponent does not reinitialize. Is it an issue with angular router?

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

推荐答案

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

This is currently the only supported behavior. There are plans to make this configurable https://github.com/angular/angular/issues/9811

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

You can subscribe to parameters change and do the initialization there

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

    }
}

另请参见如何重新渲染

这篇关于Angular2组件不会针对参数化路线重新初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆