Angular 2 reload route on param change

后端 未结 13 1132
小鲜肉
小鲜肉 2020-12-07 13:15

I am currently writing my first Angular 2 Application. I have an OverviewComponent which has the following simple template:

13条回答
  •  长情又很酷
    2020-12-07 13:51

    Is possible to detect any change in the parameters received, in my case I load the information using a Resolve so I don't need the parameter (only detect if it change). This is my final solution:

    public product: Product;
    private parametersObservable: any;
    
    constructor(private route: ActivatedRoute) {
    }
    
    ngOnInit() {
      this.parametersObservable = this.route.params.subscribe(params => {
        //"product" is obtained from 'ProductResolver'
        this.product = this.route.snapshot.data['product'];
      });
    }
    
    //Don't forget to unsubscribe from the Observable
    ngOnDestroy() {
      if(this.parametersObservable != null) {
        this.parametersObservable.unsubscribe();
      }
    }
    

提交回复
热议问题