1

I want to load component based on URL parameter when the user clicks on a link in the home component. For exmaple user is at "/home" which have a list of links

Link 1 Link 2

When the user clicks on Link 1 or 2, I want to load the details component and the url should be '/home/details/1' the "1" in the end is a route parameter.

Same thing happens when user clicks on Link 2, same details component is loaded but the parameter is "2" and url changes to '/home/details/2'

I have tried the following in routing module, the url changes but details component does not loads and no error is reported in the console.

const routes: Routes = [
  {
     path:'',
     component:HomeComponent,
     children:[
      {
        path:':id',
        component:DetailsComponent
      }]
   }
]

And the anchor tags are:

<a [routerLink]="[1]">Link 1</a>
<a [routerLink]="[2]">Link 2</a>
2
  • How are you calling the DetailsComponent? Can you share that code as well? Commented Jan 3, 2022 at 23:09
  • I am calling the detail component like <a [routerLink]="['detail', 1 ]">Link 1</a> Commented Jan 4, 2022 at 1:40

2 Answers 2

2

If you want to redirect to certain pages you can always use this

In RouteModule
    const routes: Routes = [
      {
         path:'Home',
         component:HomeComponent,
         children:[
          {
            path:':id',
            component:DetailsComponent
          }]
       }
    ]

    In Html

    <a [routerLink] = ['/Home','1'] >Link 1</a>
     <a [routerLink] = ['/Home','2']>Link 2</a>
     
  

Sign up to request clarification or add additional context in comments.

1 Comment

I tried the above example, only URL changes, the DetailsComponent does not show.
1

Try <a [routerLink]="['/home',2]">Link 2</a> and

const routes: Routes = [
  {
     path:'home',
     component:HomeComponent,
     children:[
      {
        path:':id',
        component:DetailsComponent
      }]
   }
]

Also you should generate your pages with the following command:

ng g module detail --module person --route {personid}/detail

1 Comment

I cannot use 'path:'home', as the home is inside an angular module

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.