2017-08-22 96 views
0

我使用ng serve,並有這樣的路由配置角4不能

export const appRoutes: Routes = [ 
    { 
    path: '', 
    children: [ 
     { 
     path: '', 
     component: AuthComponent, 
     children: [ 
      { 
      path: 'login', 
      component: LoginComponent 
      }, 
      { 
      path: 'registration', 
      component: RegistrationComponent 
      } 
     ] 
     }, 
     { 
     path: 'restore', 
     component: RestoreComponent 
     }, 
     { 
     path: 'newpass', 
     component: NewPassComponent 
     } 
    ] 
    } 
]; 

和auth.component.html兩粒扣

<nav> 
    <ul> 
    <li><a routerLink="/login" routerLinkActive="active-link">Login</a></li> 
    <li><a routerLink="/registration" routerLinkActive="active-link">Sign up</a></li> 
    </ul> 
</nav> 

當我點擊登錄鏈接,瀏覽角度我匹配任何路線登錄頁面沒有錯誤,但當我重新加載localhost:4200/login頁面,我得到這個錯誤

Console screen

但頁面加載成功,組件將工作。

那麼,我該如何解決它,爲什麼角度使第二個NavigationStart /登錄/(登錄)?

+0

,你可以嘗試使用'HashLocationStrategy' –

+0

@ n00dl3但是當我刷新/重新加載頁面,角度沒有給出錯誤 – Ruslan

回答

0

好的。我解決了這個問題。在AuthComponent我有這樣的代碼

let isAuthorized = await this.checkAuthorization(); 
if (isAuthorized) { 
    this.router.navigate(['dashboard'], {relativeTo: this.route}); 
} else { 
    this.router.navigate(['login'], {relativeTo: this.route}); 
} 

,所以我做的重定向/登錄/註冊/登錄,但航線的配置可是沒有這條道路

0

你不需要使用路徑「」的孩子。嘗試使用這個:

export const appRoutes: Routes = [ 
    { 
    path: 'pathToAuth', // change to your path 
    component: AuthComponent, 
    children: [ 
     { 
     path: 'login', 
     component: LoginComponent 
     }, 
     { 
     path: 'registration', 
     component: RegistrationComponent 
     } 
    ] 
    }, 
    { 
    path: 'restore', 
    component: RestoreComponent 
    }, 
    { 
    path: 'newpass', 
    component: NewPassComponent 
    } 
]; 
+0

將'pathToAuth'更改爲'',但是如果pathToAuth ===「」嘗試使用沒有子元素,錯誤仍然會留下 – Ruslan

+0

:export const appRoutes:Routes = [ { path:'' , 組件:AuthComponent, { path:'login', 組分:LoginComponent }, { 路徑: '登記', 組分:RegistrationComponent }, { 路徑: '恢復', 組分:RestoreComponent }, { 路徑: 'newpass', 部件:NewPassComponent } ];格式 –

+0

對不起,但是LoginComponent和RegistrationComponent是一個表單,而AuthComponent是一個包含表單的常規佈局。所以當我打開/登錄頁面時,LoginComponent必須在AuthComponent – Ruslan