0

I have an app containing 2 routes :

const routes: Routes = [
  { path: "", component: HomeComponent },
  { path: "hero", component: HeroComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

The documentation says :

The path-matching strategy, one of 'prefix' or 'full'. Default is 'prefix'.

By default, the router checks URL elements from the left to see if the URL matches a given path, and stops when there is a match. For example, '/team/11/user' matches 'team/:id'.

The path-match strategy 'full' matches against the entire URL. It is important to do this when redirecting empty-path routes. Otherwise, because an empty path is a prefix of any URL, the router would apply the redirect even when navigating to the redirect destination, creating an endless loop.

By default, the pathMatch is prefix, so I expected the first route { path: "", component: HomeComponent } to match everything as an empty string is a prefix of all paths.

When I hit /hero in the browser, I can see the HeroComponent, when I hit /foo, I see an error in the logs saying the route was not found. So obviously the first route isn't picked by the angular router despite its default prefix path-matching.

I also tried with these routes :

const routes: Routes = [
  {
    path: "",
    redirectTo: "/hero",
    pathMatch: "prefix"
  },
  { path: "hero", component: HeroComponent }
];

to reproduce the behavior described in the documentation

Otherwise, because an empty path is a prefix of any URL, the router would apply the redirect even when navigating to the redirect destination, creating an endless loop.

When I hit an empty url /, the redirection works fine and I'm redirected to the /hero path, I expected to have a endless loop.

Here is the app on stackblitz using the angular v11.0.8

Can someone explain me how does the angular router path matching works exactly ? How can I obtain a endless loop (just to understand how the router works, not for a real app) ?

Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
  • `{ path: "**" }` is a wild card if you want to match everything: https://angular.io/guide/router#setting-up-wildcard-routes – Get Off My Lawn Apr 04 '21 at 14:24
  • I know, but what is the difference between `pathMatch: "prefix"` and `pathMatch: "full"` ? I tried both values and I see no difference. Why `{path: "", pathMatch: "prefix"}` doesn't match all routes ? When I look into the [documentation](https://angular.io/guide/router#setting-up-redirects) they use `pathMatch: 'full'`, I tried with `'prefix'` and I see the same result... – Olivier Boissé Apr 04 '21 at 14:25
  • Look at this answers https://stackoverflow.com/a/42992231 – Smollet777 Apr 04 '21 at 14:46
  • Does this answer your question? [In Angular, What is 'pathmatch: full' and what effect does it have?](https://stackoverflow.com/questions/42992212/in-angular-what-is-pathmatch-full-and-what-effect-does-it-have) – Smollet777 Apr 04 '21 at 14:49
  • no I already saw this answer, I still don't understantd why `{ path: '', redirectTo: 'welcome', pathMatch: 'prefix' },` doesn't match all the routes (even if I omit the `path: "**"` route) ? an empty path is a prefix of all paths so it should match all the undeclared paths – Olivier Boissé Apr 04 '21 at 14:52
  • The answer said `pathMatch = 'prefix' tells the router to match the redirect route when the remaining URL begins with the redirect route's prefix path.`. If you look at my first example, this is obviously not the case. When I hit `/foo` path, the first route should have been chosen – Olivier Boissé Apr 04 '21 at 14:57
  • Look at this [answer](https://stackoverflow.com/a/62476799/5521607) at the section `What if there was no pathMatch: 'full'?`. I tried without `pathMatch: 'full'` (see [my example](https://stackblitz.com/edit/angular-empty-path-not-found?file=src/app/app-routing.module.ts) in the question) and I don't get a endless loop. – Olivier Boissé Apr 04 '21 at 15:06
  • I think I saw my mistake, I wrote `redirectTo: "hero"` instead of redirectTo: "/hero". I still doesn't get a endless loop but when I hit `/foo`, I am redirected to the `/foo` url. Maybe angular router has changed so it prevents the endless loop to occur – Olivier Boissé Apr 04 '21 at 15:24

0 Answers0