Angular 2 router.navigate

Spa*_*ers 46 javascript angular2-routing angular

我正在尝试使用路径和查询参数的混合导航到Angular 2中的路线.

以下是路径是路径最后一部分的示例路由:

{ path: ':foo/:bar/:baz/page', component: AComponent }
Run Code Online (Sandbox Code Playgroud)

试图像这样链接使用数组:

this.router.navigate(['foo-content', 'bar-contents', 'baz-content', 'page'], this.params.queryParams)
Run Code Online (Sandbox Code Playgroud)

我没有得到任何错误,从我能理解的这应该工作.

Angular 2文档(目前)以下为例:

{ path: 'hero/:id', component: HeroDetailComponent }

['/hero', hero.id] // { 15 }
Run Code Online (Sandbox Code Playgroud)

谁能看到我哪里出错了?我在路由器3上.

Gün*_*uer 84

如果第一段不是/以相对路线开始,则它是相对路线.router.navigate需要relativeTo相对导航的参数

要么让路线绝对:

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

或者你通过 relativeTo

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

也可以看看


yal*_*esh 12

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'];
        });
}
Run Code Online (Sandbox Code Playgroud)

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