2017-02-16 64 views
3

我的路線:找不到頁面處理,而不改變URL角2

const routes: Routes = [ 
     { path: '', component: DashboardComponent, canActivate: [RouteGuard, AuthGuard] }, 
     { path: 'signup', component: SignUpTenantComponent, canActivate: [RouteGuard] }, 
     { path: 'signin', component: SignInComponent, canActivate: [RouteGuard] }, 
     { path: ':url', component: DashboardComponent, canActivate: [RouteGuard, AuthGuard] }, 
     { path: 'page-not-found', component: PageNotFoundComponent }, 
     { path: '**', redirectTo: 'page-not-found' } 
    ]; 

而且我有守衛在那裏我檢查的網址,如果它是錯誤的,那麼在PageNotfoundComponent重定向:

this.router.navigate(['page-not-found']); 

而作爲一個結果我在url localhost:port/page-not-found中看到。但是我想在不改變url的情況下做到這一點。 如果我將{ skipLocationChange: true }添加到router.navigate那麼我會看到主要的網址localhost:port/。 我想查看錯誤的網址,例如localhost:port/rgqadffPageNotFoundComponent的內容。

我該如何解決?

+0

不知道,但嘗試路線的最後一個對象改變爲:{路徑:' **',組件:PageNotFoundComponent} –

+0

如果你有東西,請重播。 –

+0

它沒有幫助我 –

回答

2

我使用以下方式和正常工作,無論你寫它會顯示NotFoundComponent不改變網址

const APP_ROUTES: Routes = [ 
    {path: '' , component: HomeComponent }, 
    {path: 'about', component: AboutComponent }, 
    {path: '**', component: NotFoundComponent } 
]; 
+0

是的,但我需要從服務的404頁面重定向。你會怎麼做? –

+0

this.router.navigate(['/ 404']) –

+0

在反斜槓將您重定向到之後寫入任何內容。並使用{path:'**',component:PageNotFoundComponent}替換路由數組中的{path:'page-not-found',組件:PageNotFoundComponent} –