1

I am using JSON server fake rest API in angular14 and creating a dynamic menu with JSON file and it perfectly works but I just want to route the menu when the user clicks menu it will be routed the same. I am sharing the code for your convenience.

So, does anyone have a solution to this please let me know. Thank you

db.json

  "users": [
    {
      "displayName": "Home",
      "routerLink": "become-angular-tailer"
    },
    {
      "displayName": "Services",
      "url": "https://blog.angular.io/"
    },
    {
      "displayName": "How it Works",
      "routerLink": "become-angular-tailer"
    },
    {
      "displayName": "About",
      "routerLink": "become-angular-tailer"
    }
  
  ],

Header component.html

     <li class="nav-item active" *ngFor="let users of user">
    
    
            <a class="nav-link" href="#"> {{ users.displayName }}
              {{ users.title }}
    
            </a>
    
       </li>

Header.component.ts

this.service.getPosts().subscribe(response => {
          this.user = response;
});

2 Answers 2

1
   <li class="nav-item active" *ngFor="let users of user">
    <a class="nav-link" [routerLink]="{{users.routerLink}} >
      {{ users.title }}
    </a>
   </li>

      // and in app-routing-module.ts 

 const routes: Routes = [
 { path: 'item.routerLink1', component: 'FirstComponent' },
 { path: 'item.routerLink2', component: 'SecondComponent' }];


 @NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
  }) 
Sign up to request clarification or add additional context in comments.

Comments

0

header.component.html

<li class="nav-item active " *ngFor="let users of user">
   <a class="nav-link" ariaCurrentWhenActive="page" [routerLink]="users.routerLink"> {{ users.displayName }}
   {{ users.title }}
   </a>
</li>

db.json

"users": [
    {
      "displayName": "Home",
      "routerLink": "home"

    },
    {
      "displayName": "Services",
      "routerLink": "service"

      
    },
    {
      "displayName": "How it Works",
      "routerLink": "howitwork"
    },
    {
      "displayName": "About",
      "routerLink": "about-us"
    },
    {
      "displayName": "Download App",
      "routerLink": "download"
    },
    {
      "displayName": "Feature",
      "routerLink": "features"
    },
    {
      "displayName": "FAQ",
      "routerLink": "faq"
    }
  ],

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.