Angular 2路由器与路径不匹配

时间:2016-12-26 10:17:11

标签: angular angular2-routing

当我尝试在我的视图中加载 Plunkr PageNotFoundComponent中的以下代码时:

src / app-routing.module(摘录)

const appRoutes: Routes = [
  { path: '', redirectTo: '/one', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

当我删除wildcard路径时,我得到了所需的结果(视图中加载了组件one):

const appRoutes: Routes = [
  { path: '', redirectTo: '/one', pathMatch: 'full' },
];

我错过了什么?

这里是一名队员https://embed.plnkr.co/MxM5JCJmwHVVSuTdfWZg/

1 个答案:

答案 0 :(得分:1)

像这样更改@NgModule

@NgModule({
  imports: [ 
    BrowserModule,
    OneModule,//<- before root routing module
    AppRoutingModule, 

  ],
  declarations: [ AppComponent, PageNotFoundComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

由于OneModule位于AppRoutingModule之后,OneModule中的路径会在AppRoutingModule's通配符路径中丢失。