在angular2中,如果路线不存在,我希望用户定向到主页
我使用的是最新的angular2和打字稿。
我是否将此代码添加到某处的应用路由文件中?
这是我的路线文件: - 如何更新此文件以执行此操作:
import { Routes } from '@angular/router';
import { HomeRoutes } from './components/home/index';
import { ContactRoutes } from './components/contact/index';
export const routes: Routes = [
...HomeRoutes,
...ContactRoutes
];
这是我的主页路线:
import { Route } from '@angular/router';
import { HomeComponent } from './index';
export const HomeRoutes: Route[] = [
{
path: '',
component: HomeComponent
},
{
path: 'home',
component: HomeComponent
},
{
path: 'homepage',
component: HomeComponent
},
{
path: '**',
component: PageNotFoundComponent
}
];
答案 0 :(得分:3)
设置路线时,在模块中完成此操作。注意路线与' **'路径。您可以将其设置为您喜欢的任何组件。
请参阅:https://angular.io/docs/ts/latest/guide/router.html
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
{ path: 'hero/:id', component: HeroDetailComponent },
{ path: 'crisis-center', component: CrisisListComponent },
{
path: 'heroes',
component: HeroListComponent,
data: {
title: 'Heroes List'
}
},
{ path: '', component: HomeComponent },
{ path: '**', component: PageNotFoundComponent }
])
],
declarations: [
AppComponent,
HeroListComponent,
HeroDetailComponent,
CrisisListComponent,
PageNotFoundComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule {
}