Angular 2 router.navigate

前端 未结 2 1114
清歌不尽
清歌不尽 2020-12-07 23:57

I\'m trying to navigate to a route in Angular 2 with a mix of route and query parameters.

Here is an example route where the route is the last part of the path:

2条回答
  •  星月不相逢
    2020-12-08 00:44

    If the first segment doesn't start with / it is a relative route. router.navigate needs a relativeTo parameter for relative navigation

    Either you make the route absolute:

    this.router.navigate(['/foo-content', 'bar-contents', 'baz-content', 'page'], this.params.queryParams)
    

    or you pass relativeTo

    this.router.navigate(['../foo-content', 'bar-contents', 'baz-content', 'page'], {queryParams: this.params.queryParams, relativeTo: this.currentActivatedRoute})
    

    See also

    • https://github.com/angular/angular.io/blob/c61d8195f3b63c3e03bf2a3c12ef2596796c741d/public/docs/_examples/router/ts/app/crisis-center/crisis-detail.component.1.ts#L108
    • https://github.com/angular/angular/issues/9476

提交回复
热议问题