在angular2中,如果路径不存在,我希望将用户定向到主页
在angular2中,如果路径不存在,我希望将用户定向到主页 我用的是最新的angular2和打字稿 我是否将此代码添加到应用程序路由文件的某个位置 这是我的路由文件:-如何更新此文件以实现此目的:在angular2中,如果路径不存在,我希望将用户定向到主页,angular,Angular,在angular2中,如果路径不存在,我希望将用户定向到主页 我用的是最新的angular2和打字稿 我是否将此代码添加到应用程序路由文件的某个位置 这是我的路由文件:-如何更新此文件以实现此目的: import { Routes } from '@angular/router'; import { HomeRoutes } from './components/home/index'; import { ContactRoutes } from './components/contact/i
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
}
];
这是我的主页路线:
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
}
];
这是在设置路由时在模块中完成的。请注意带有“**”路径的路线。您可以将其设置为您喜欢的任何组件 见:
@NgModule({
进口:[
浏览器模块,
FormsModule,
RouterModule.forRoot([
{path:'hero/:id',component:HeroDetailComponent},
{路径:“危机中心”,组件:CrisisListComponent},
{
路径:'英雄',
组件:HeroListComponent,
数据:{
标题:“英雄榜”
}
},
{路径:'',组件:HomeComponent},
{路径:'**',组件:PageNotFoundComponent}
])
],
声明:[
应用组件,
HeroListComponent,
他说,,
CrisilistComponent,
PageNotFoundComponent
],
引导:[AppComponent]
})
导出类AppModule{
}
我在问题中添加了我的路由文件,我将如何更新该文件?还有什么是PageNotFoundComponent?喜欢其中的什么?@AngularMPageNotFoundComponent
可以是属于您的应用程序的任何组件。在您的情况下,如果您想将不存在的路径重定向到那里,您需要将其替换为HomeComponent
。我这样做了,然后停止了其他路由的工作,例如联系路由它是列表中的最后一条路由吗?