在这里,将pathmatch完全使用,当我删除此pathmatch时,它甚至不会加载应用程序或运行项目
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { WelcomeComponent } from './home/welcome.component';
/* Feature Modules */
import { ProductModule } from './products/product.module';
@NgModule({
imports: [
BrowserModule,
HttpModule,
RouterModule.forRoot([
{ path: 'welcome', component: WelcomeComponent },
{ path: '', redirectTo: 'welcome', pathMatch: 'full' },
{ path: '**', redirectTo: 'welcome', pathMatch: 'full' }
]),
ProductModule
],
declarations: [
AppComponent,
WelcomeComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
最佳答案
RouterModule.forRoot([
{ path: 'welcome', component: WelcomeComponent },
{ path: '', redirectTo: 'welcome', pathMatch: 'full' },
{ path: '**', component: 'pageNotFoundComponent' }
])
案例1
pathMatch:'full'
:在这种情况下,在
localhost:4200
(或某些服务器)上启动应用程序时,默认页面将为欢迎屏幕,因为url为https://localhost:4200/
如果是
https://localhost:4200/gibberish
,则由于path:'**'
通配符,它将重定向到pageNotFound屏幕案例2
pathMatch:'prefix'
:如果路由具有
{ path: '', redirectTo: 'welcome', pathMatch: 'prefix' }
,则此路由将永远不会到达通配符路由,因为每个URL都将与定义的path:''
匹配。