0

dashboard component is global component. users module is lazy loaded which contains login component. I want to access this dashboard component in the lazy loaded login component. How do I achieve it? I know we can use Shared module. But, I am not sure how to implement it exactly. Please, guide me through.

Dashboard.componenet.html

<div class="container-fluid">
  <div class="row">
    <div class="col-md-3">
      <p-menu [model]="menuItems"></p-menu>
    </div>
    <div class="col-md-9">

    </div>
  </div>
</div>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

//PrimeNG
import { MenuModule } from 'primeng/primeng';

//components
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';

const appRoutes = [
  { path: '', component: DashboardComponent },
  { path: 'users', loadChildren: 'app/users/users.module#UsersModule'}
]

@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    MenuModule,
    RouterModule.forRoot(appRoutes)
  ],
  exports:[DashboardComponent],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

users.module.ts

//Interfaces
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

//Components
import { LoginComponent } from './login/login.component';

const lazyRoutes = [
    {path:'login/login', component: LoginComponent}
]

@NgModule({
    declarations:[
        LoginComponent
        // DashboardComponent
    ],
    imports: [
        // AppModule,
        RouterModule.forChild(lazyRoutes)
    ],
    exports:[],
    providers: []
})

export class UsersModule {}

login.component.html

<dashboard></dashboard>[enter image description here][1]
3
  • It may not be a good idea to try to reference the Dashboard component from one of your lazy loaded components. What are you trying to accomplish? Maybe there is another way? Commented Feb 10, 2018 at 18:49
  • I am trying to make a menu inside dashboard. And I want to share that menu throughout my application, even in my lazyloaded module. Commented Feb 10, 2018 at 18:55
  • Add a router outlet to the dashboard and route everything into that router outlet. Then everything will share that menu without having to add it to each component. Commented Feb 10, 2018 at 18:59

1 Answer 1

2

In my code, I have a "Shell" component similar in concept to your dashboard. It looks like this:

shell component

<mh-menu></mh-menu>

<div class='container'>
    <router-outlet></router-outlet>
</div>

The <mh-menu> is the component containing the menu. Below the menu is a router outlet. I can then route any of my content to that router outlet so it appears below the menu and the menu appears on every page.

In my example, my products are a lazy loaded module, yet I'm able to route the product edit page into this primary router outlet. So the lazy loaded component appears with the menu.

Here is a picture:

enter image description here

I have a complete example here: https://github.com/DeborahK/MovieHunter-routing (though its movies and not products)

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

2 Comments

This worked like charm! Thank you @DeborahK. But, I would still like to know if we want to share that component in the lazy loaded component. How is that done? I know we can using shared module. But, I dont know the specifics.
Yes, you can build a shared module to share components. If you look in the link I provided above under the shared folder you will see an example.

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.