2017-02-16 34 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} –