Angular2:'...'的路由生成器未包含在传递的参数中

Inn*_*ve1 1 routing angular2-routing angular

我在使用Angular2路线时遇到了麻烦.我有几个级别的嵌套.这是我的设置:我为未经身份验证的用户提供了最高级别的路由.登录后,他们就在/my路线下.在那个级别上,他们有一个仪表板,可以将它们带到/my/projects/关卡.路由下projects需要id.设置细节如下.这是最高级别AppComponent:

 @RouteConfig([
  {path: '/home', name:'Home', component: HomeComponent, useAsDefault: true},
  {path: '/login', name: 'Login', component: LoginComponent},
  {path: '/signup', name: 'SignUp', component:SignUpComponent},
  {path: '/my/...', name: 'MyApp', component: MyAppComponent}
])
export class AppComponent {
  constructor(private router: Router) {

  }
Run Code Online (Sandbox Code Playgroud)

经过身份验证后,用户将转到MyAppComponent,其设置如下:

 @RouteConfig([
  {path:'/dashboard', name: 'MyDashboard', component: DashboardComponent, useAsDefault: true},
  {path: '/projects/...', name: 'MyProjects', component: ProjectRootComponent},
])

export class MyAppComponent {

  constructor(private router: Router) {
  }
}
Run Code Online (Sandbox Code Playgroud)

DashboardComponent:

export class DashboardComponent {
  public projects: Array<Project>;
  public loaded: boolean;
  public loadError: boolean;

  constructor(private _service: ProjectService, private router:Router) {
    this.projects = [];
    this.loaded = false;
  }

  ngOnInit() {
    console.log('Dashboard component initialized');
    this._service.getUserProjects()
    .then(projects => {
      this.loaded = true;
      this.projects = projects;
    },
    error => {
      this.loaded = true;
      this.loadError = error;
    });
  }

  selectProject(project: Project) {
    // this.router.navigateByUrl('/my/projects/' + project.id); // Tried this approach as well with the same error
    this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}]);
  }
Run Code Online (Sandbox Code Playgroud)

进入DashboardComponent后,我加载了一个项目列表.当我单击一个项目时,该selectProject(project)方法被调用,该方法应该导航到该项目页面.该项目id作为参数传入.MyProjects路由需要一个id,并设置如下:

    @RouteConfig ([
  {path: '/:id', name: 'ProjectHome', component: ProjectHomeComponent, useAsDefault: true},
  {path: '/:id/details', name: 'ProjectDetails', component: ProjectDetailsComponent },
])

export class ProjectRootComponent implements OnInit {

  id: string;
  constructor(private router: Router, private routeParams: RouteParams, private projectService: ProjectService) {
  }

  ngOnInit() {
    this.id = this.routeParams.get('id');

    console.log('Project Component Initialized with id: ' + this.id);
  }

  createProject() {

  }
}
Run Code Online (Sandbox Code Playgroud)

selectProject(project)调用仪表板时,我收到错误:ORIGINAL EXCEPTION: Route generator for 'id' was not included in parameters passed.

我还尝试使用以下两种方法直接导航到项目详细信息:

this.router.parent.navigate(['MyApp', 'MyProjects', {id: project.id}, 'ProjectDetails']
Run Code Online (Sandbox Code Playgroud)

 this.router.parent.navigate(['MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
Run Code Online (Sandbox Code Playgroud)

两者都产生与上面相同的错误.

注意:这与前面提到的这个问题类似,但我的路线已经按照该问题的答案中的建议设置.

Sas*_*sxa 7

我认为您有路由查找问题,因为您从子路由导航.来自RouterLinkdocs(应该是相同的linkParams):

第一个路由名称应该以/,./或../为前缀.如果路由以/开头,路由器将从应用程序的根目录查找路由.如果路由以./开头,则路由器将查找当前组件的子路由.如果路由以../开头,路由器将查看当前组件的父节点.

我相信这应该有效:

this.router.navigate(['/MyApp', 'MyProjects', 'ProjectDetails', {id: project.id}]
Run Code Online (Sandbox Code Playgroud)

或从仪表板:

this.router.navigate(['./MyProjects', 'ProjectDetails', {id: project.id}]
Run Code Online (Sandbox Code Playgroud)