在这里它使用完整的路径匹配,当我删除这个路径匹配时,它甚至不会加载应用程序或运行项目
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 { }
原文由 Chanaka Amarasinghe 发布,翻译遵循 CC BY-SA 4.0 许可协议
案例 1
pathMatch:'full'
:在这种情况下,当应用程序在localhost:4200
(或某些服务器)上启动时,默认页面将是欢迎屏幕,因为 url 将是https://localhost:4200/
如果
https://localhost:4200/gibberish
这将重定向到 pageNotFound 屏幕,因为path:'**'
通配符案例2
pathMatch:'prefix'
:如果路由有
{ path: '', redirectTo: 'welcome', pathMatch: 'prefix' }
,现在这将永远不会到达通配符路由,因为每个 url 都会匹配path:''
定义。