页面未找到处理但未更改网址Angular 2

时间:2017-02-16 09:43:30

标签: angular typescript

我有路线:

    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']);

结果我在网址localhost:port/page-not-found中看到了。但我想在不改变网址的情况下这样做。 如果我将{ skipLocationChange: true }添加到router.navigate,那么我会看到主网址localhost:port/。 我想看到错误的网址,例如localhost:port/rgqadff内容为PageNotFoundComponent

我该如何解决?

1 个答案:

答案 0 :(得分:2)

我正在使用以下方式并且它工作正常,无论你写什么它都会显示NotFoundComponent而不更改url

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