带有参数的角度嵌套路线"无法匹配任何路线"

时间:2017-03-13 22:36:47

标签: javascript angularjs angular

以下是我正在处理的3条路线:

  • game/:id
  • game/:id/pricing
  • game/:id/history

我希望game/:id成为父视图,并为孩子们提供<router-outlet>以便在其中进行渲染。但是,我不断收到此错误:无法匹配任何路由。网址细分:&#39;游戏/ 1 /历史&#39;。

感谢任何帮助。

路由器模块:

import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { CommonModule } from "@angular/common";
import { GameComponent } from "./game.component";
import { GamePricingComponent } from './game-pricing.component';
import { GameHistoryComponent } from './game-history.component';

const routes: Routes = [
  { path: 'game/:id', component: GameComponent, children: [
    { path: 'pricing', component: GamePricingComponent, outlet: 'game' },
    { path: 'history', component: GameHistoryComponent, outlet: 'game' }
  ]},
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes),
    CommonModule
  ],
  declarations: [
    GameComponent,
    GamePricingComponent,
    GameHistoryComponent
  ],
  exports: [
    RouterModule
  ]
})
export class RoutingModule {}

GameComponent:

game/:id路线是父视图,其中包含<router-outlet name="game">个孩子(定价和历史记录)以进行渲染。

import { Component } from "@angular/core";

@Component({
  template: `
<div>Game stuff</div>
<router-outlet name="game"></router-outlet>
`
})
export class GameComponent {}

RootModule

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { RoutingModule } from "./routing.module";

@NgModule({
  imports: [
    BrowserModule,
    RoutingModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export default class AppModule { }

AppComponent

import { Component } from '@angular/core';

@Component({
  selector: 'app',
  template: `
<div class="base">
    <router-outlet></router-outlet>
</div>
`,
})
export class AppComponent {}

1 个答案:

答案 0 :(得分:2)

经过一段时间后,我能够通过删除<router-outlet>的命名来解决这个问题。事实证明,单个子路由器插座“正常工作” - 因此不需要命名......

然而,在编写本文时,我仍然不知道如何在子路由中使用路由器插座命名。如果有人想在这里详细说明,我会非常感激。 (也许命名仅在同一级别有超过1个出口时才有效?)