本文介绍了在Angular中,什么是“路径匹配:完整",它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里它是完整使用pathmatch的,当我删除该pathmatch时,它甚至不会加载应用程序或运行项目

In here it is use pathmatch as full and when i delete this pathmatch it doesn't even load the app or run the project

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/

Case 1 pathMatch:'full':In this case, when app is launched on localhost:4200 (or some server) the default page will be welcome screen, since the url will be https://localhost:4200/

如果https://localhost:4200/gibberish由于path:'**'通配符而将其重定向到 pageNotFound 屏幕

If https://localhost:4200/gibberish this will redirect to pageNotFound screen because of path:'**' wildcard

案例2 pathMatch:'prefix':

如果路由具有{ path: '', redirectTo: 'welcome', pathMatch: 'prefix' },由于每个URL都将与定义的path:''匹配,因此此路由将永远不会到达通配符路由.

If the routes have { path: '', redirectTo: 'welcome', pathMatch: 'prefix' }, now this will never reach the wildcard route since every url would match path:'' defined.

这篇关于在Angular中,什么是“路径匹配:完整",它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 19:43