0

I have an Angular app where the root directory is a variable route parameter:

/:demo_profile/(etc).

I cannot use relative path because my routerLink is available from different components/routes.

How can I set a routerLink target that is relative to the root of my site, followed by this param? i.e.

/SomeRoute/home

My routes:

const routes: Routes = [
  {
    path: ':demo_profile',   
    children: [
      {
        path: '',
        redirectTo: 'home',
        pathMatch: 'full'
      },
      {
        path: 'home',    
        component: HomeComponent,
      },
      {
        path: 'cards',    
        component: CardsComponent,
      },
      {
        path: 'card/:id',    
        component: CardComponent,
      },
      {
        path: 'help',    
        component: HelpComponent,
      },
      {
        path: '**',    
        redirectTo: ':demo_profile',
      }
    ]
  },
  {
    path: '\**',    
    redirectTo: '/DGInsureCo/home',
  }
];

1 Answer 1

1

While not ideal, a solution consists in fetching the current URL path and then reconstruct the router link as per the below:

routerLink in the template:

<a routerLink="/{{demo_profile}}/home">Home</a>

and then reference this.demo_profile using activatedRoute:

this.route.params.subscribe( params =>
      this.demo_profile = params['demo_profile']
    )

or:

activatedRoute.snapshot.url[0].path
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.