Use wildcard to redirect to main sub-route component in angular

Project: codever - File: public-routing.module.ts

The ** wildcard has to be defined as a child of the root you want redirected back to. Thus URLs like https://www.codever.land/snippets/60046e4554aac471e0799da7/some-mongo-title-here will still “point” to https://www.codever.land/snippets/60046e4554aac471e0799da7:

  {
    path: 'snippets/:id',
    component: PublicSnippetDetailsComponent,
    children: [
      // This is a WILDCARD CATCH-ALL route that is scoped to the "/snippets/:snippetid"
      // route prefix. It will only catch non-matching routes that live
      // within this portion of the router tree.
      {
        path: '**',
        component: PublicSnippetDetailsComponent
      }
    ]
  },

Reference - https://angular.io/guide/router#setting-up-redirects


Shared with from Codever.land. Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Subscribe to our newsletter for more code resources and news

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being