2016-11-15 38 views
1

我嘗試使用兒童路線。我的主要路由模塊(在根目錄下):angular2 route for not尋找頁面

import { NgModule }    from '@angular/core'; 
import { RouterModule, Routes } from '@angular/router'; 
import { HomeComponent } from './home/home.component'; 
import { ProfileComponent } from './profile/profile.component'; 
import { NotFoundComponent } from './notfound/notfound.component'; 
import { LoggedInGuard } from './login-guard/login-guard.component'; 

const routes: Routes = [ 
    {path: '', redirectTo: 'home', pathMatch: 'full'}, 
    {path: 'home', component: HomeComponent, pathMatch: 'full'}, 
    {path: 'profile', component: ProfileComponent, canActivate: [LoggedInGuard], canLoad: [LoggedInGuard]}, 
    {path: '**', component: NotFoundComponent}, 
]; 
@NgModule({ 
    imports: [ RouterModule.forRoot(routes) ], 
    exports: [ RouterModule ] 
}) 
export class AppRoutingModule {} 

和兒童路由模塊(在子目錄):

import { NgModule }  from '@angular/core'; 
import { RouterModule } from '@angular/router'; 

import { PPComponent } from './pp.component'; 
import { MembersComponent } from './members/members.component'; 
import { HistoryComponent } from './history/history.component'; 


@NgModule({ 
    imports: [ 
    RouterModule.forChild([ 
     { 
     path: 'partner-program', 
     component: PPComponent, 
     children: [ 
      { 
      path: '', 
      redirectTo: 'members', 
      pathMatch: 'full' 
      }, 
      { 
      path: 'members', 
      component: MembersComponent, 
      }, 
      { 
      path: 'history', 
      component: HistoryComponent, 
      }, 
     ] 
     }, 


    ]) 
    ], 
    exports: [ 
    RouterModule 
    ] 
}) 
export class PPRoutingModule { } 

我有一個關於這條路線{path: '**', component: NotFoundComponent}問題。 Angular2在任何兒童路線之前都會看到這條路線。結果url'http://localhost:3000/partner-program'顯示notFound組件。如果我刪除找不到路由,'http://localhost:3000/partner-program'工作正常。 我怎樣才能聲明notFound路線,並說Angular2在最後一個回合(在子路線之後)檢查它?

GünterZöchbauer,你的意思是這樣的嗎?

RouterModule.forChild([ 
     { 
     path: '', 
     children: [ 
      { 
      path: 'partner-program', 
      component: PPComponent, 
      children: [ 
       { 
       path: '', 
       redirectTo: 'members', 
       pathMatch: 'full' 
       }, 
       { 
       path: 'members', 
       component: MembersComponent, 
       }, 
       { 
       path: 'history', 
       component: HistoryComponent, 
       }, 
      ] 
      } 
     ] 
     } 
    ]) 
+0

我認爲你的'PPRoutingModule'路線沒有正確配置爲子路線。小孩路線必須是父母路線的孩子。 ''http:// localhost:3000/partner-program''只能在路徑爲'{path:'',redirectTo:'home',pathMatch:'full'},'(空路徑)它不是。 –

回答

5

當您導入這些模塊到主模塊,請確保AppRoutingModule是在最後。這是關於路線登記訂單

+0

omg,非常感謝。有用! –