Angular 2 reload route on param change

后端 未结 13 1129
小鲜肉
小鲜肉 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:32

    I don't know whether there is an answer to the problem similar to the one I'm going to propose right here, so I'll do it anyway:

    I managed to achieve a 'fake' reload the following way.

    What I basically did is creating a Component which redirects me to the 'real' component I want to use:

    @Component({
      selector: 'camps-fake',
      template: ''
    })
    export class FakeComponent implements OnInit {
    
      constructor(private _router:Router,
                  private _route:ActivatedRoute)
      { }
    
      ngOnInit() {
        let id:number = -1;
        this._route.params.forEach((params:Params) => {
          id = +params['id'];
        });
    
        let link:any[] = ['/details', id];
        this._router.navigate(link);
      }
    
    }
    

    So by selecting a list item the router will navigate to /fake/:id which just extracts the id from the URL and navigates to the 'real' component.

    I know there might be an easier, or more fancy way, but I think this solution works pretty well since the fake doesn't really attract attention. Just the 'flashing' when the page reloads is a negative aspect but as far as my css knowledge reaches there might be some transition to cover that.

提交回复
热议问题