How to dynamically load different forms in material sidenav

I have multiple modules that are lazily loaded and they have their own routing.
For example,

UserModule has the following routes
user/list
user/create
user/edit/:userId

And to view the data when we click on a grid item from user-list page, I used to open a mat-dialog and show the data.

Now the requirements has changed and we would like to show the same view-component in a right side sidenav.

One approach I thought of is having

<mat-sidenav position="end" fixedInViewport="true" #sidenav mode="side"> 
    <app-user-view [user]="selectedRow"></app-user-view>
</mat-sidenav>

But this is like hidden in the user list HTML page. And also, To close, I have to either pass the sidenav ref in my view component or call and @Output emit.

Can we have a global sidenav next to the main left-side sidenav that dynamically loads my view components from any of my lazily loaded modules?

The approach you’ve mentioned with a mat-sidenav and an app-user-view component inside it is a reasonable way to display the user details in a side panel. However, there are some considerations to keep in mind when implementing this:

  1. Communication between Components: You’ll need a way to communicate between the user-list component (where you likely have your grid) and the app-user-view component within the mat-sidenav. One common approach is to use a service to share data between components or to emit events.

  2. Lazy Loading: If you’re using lazy loading for your modules, make sure that the app-user-view component is also loaded lazily. This way, it will only be fetched when needed, reducing initial page load times.

  3. Routing Configuration: Ensure that your routing configuration is set up to display the mat-sidenav with the app-user-view component when a user clicks on a grid item. You may need to set up a route that maps to this specific view and pass any necessary parameters, like the userId, as route parameters or through a service.

Here’s a high-level example of how you might structure your code:

// user-list.component.ts
@Component({
  // ...
})
export class UserListComponent {
  // ...
  openUserDetails(userId: string) {
    // Use a service or event emitter to communicate with app-user-view
  }
}

// app-user-view.component.ts
@Component({
  // ...
})
export class AppUserViewComponent {
  @Input() user: User; // Input to display user details

  // ...
}

// app-routing.module.ts
const routes: Routes = [
  // ...
  {
    path: 'user/view/:userId', // Define a route for viewing user details
    component: AppUserViewComponent,
  },
  // ...
];

In this example, when a user clicks on a grid item in user-list, you can navigate to the user/view/:userId route and pass the userId as a route parameter. The AppUserViewComponent associated with this route will be displayed within the mat-sidenav.

Remember to adapt this to your specific project structure and requirements. Additionally, if you’re using lazy loading, ensure that you configure your routes and module imports accordingly to optimize performance.

Leave a Comment