In Angular, What is 'pathmatch: full' and what effect does it have?

Here, pathmatch is used as full, and when I delete this path, it does not even load the application or start 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 { } 
+70
source share
2 answers

pathMatch = 'full' causes a route to hit when the remaining unmatched URL segments match the prefix path

pathMatch = 'prefix' tells the router to map the redirect route when the remaining URL starts with the redirect route prefix path.

Link: https://angular.io/guide/router#set-up-redirects

pathMatch: 'full' means that the entire URL path must match and is used by the route matching algorithm.

pathMatch: 'prefix' means that the first route is selected where the path matches the beginning of the URL, but then the route matching algorithm continues to search for matching child routes where the rest of the URL matches.

+68
source
 RouterModule.forRoot([ { path: 'welcome', component: WelcomeComponent }, { path: '', redirectTo: 'welcome', pathMatch: 'full' }, { path: '**', component: 'pageNotFoundComponent' } ]) 

Case 1 pathMatch:'full' : in this case, when the application runs on localhost:4200 (or on some server), the default page will be the welcome screen, since the URL will be https://localhost:4200/

If https://localhost:4200/gibberish it will be redirected to the pageNotFound screen due to the wildcard path:'**'

Case 2 pathMatch:'prefix' :

If the routes have { path: '', redirectTo: 'welcome', pathMatch: 'prefix' } , now this will never reach the wildcard route, since each URL will correspond to path:'' defined.

+68
source

Source: https://habr.com/ru/post/1265853/


All Articles