Angular 2 router.navigate

前端 未结 2 1104
清歌不尽
清歌不尽 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
    0 讨论(0)
  • 2020-12-08 00:49

    import { ActivatedRoute } from '@angular/router';
    
    export class ClassName {
      
      private router = ActivatedRoute;
    
        constructor(r: ActivatedRoute) {
            this.router =r;
        }
    
    onSuccess() {
         this.router.navigate(['/user_invitation'],
             {queryParams: {email: loginEmail, code: userCode}});
    }
    
    }
    
    
    Get this values:
    ---------------
    
    ngOnInit() {
        this.route
            .queryParams
            .subscribe(params => {
                let code = params['code'];
                let userEmail = params['email'];
            });
    }

    Ref: https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html

    0 讨论(0)
提交回复
热议问题