Send data through routing paths in Angular

前端 未结 6 1386
无人共我
无人共我 2020-11-22 08:52

Is there anyway to send data as parameter with router.navigate? I mean, something like this example, as you can see the route has a data parameter, but doing this it\'s not

6条回答
  •  礼貌的吻别
    2020-11-22 09:39

    There is a new method what came with Angular 7.2.0

    https://angular.io/api/router/NavigationExtras#state

    Send:

    this.router.navigate(['action-selection'], { state: { example: 'bar' } });
    

    Receive:

    constructor(private router: Router) {
      console.log(this.router.getCurrentNavigation().extras.state.example); // should log out 'bar'
    }
    

    You can find some additional info here:

    https://github.com/angular/angular/pull/27198

    The link above contains this example which can be useful: https://stackblitz.com/edit/angular-bupuzn

提交回复
热议问题