简体   繁体   English

角度2/4:与延迟加载的模块及其组件共享一个组件

[英]Angular 2/4: Share a component with lazy loaded module and its components

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 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 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 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 login.component.html

<dashboard></dashboard>[enter image description here][1]

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. <mh-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: 这是一张图片:

在此处输入图片说明

I have a complete example here: https://github.com/DeborahK/MovieHunter-routing (though its movies and not products) 我在这里有一个完整的示例: https : //github.com/DeborahK/MovieHunter-routing (尽管它的电影而不是产品)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM