简体   繁体   English

我如何将延迟加载模块中的组件用作其他未延迟加载的组件中的子组件?

[英]How can i use component from lazy loaded module as child in other component which is not lazy loaded?

I have one lazy loaded module.Part of that module is CreateEmployeeComponent.I have also home component which is not lazy loaded,it's route path is defined in the app-routing.module.ts file.When i try to use the CreateEmployeeComponent as child in the Home component i get error app-create-employee is not a known element which is expected because app.module.ts dont know nothing about that CreateEmployeeComponent is declared in the declarations array in my EmployeeModule.If i dont use lazy loading i will insert the EmployeeModule in the app.module.ts file and the error will be fixed.I can't insert now EmployeeModule in my app.module.ts because then he will not be lazy loaded.我有一个延迟加载的模块。该模块的一部分是 CreateEmployeeComponent。我还有非延迟加载的主组件,它的路由路径在 app-routing.module.ts 文件中定义。当我尝试将 CreateEmployeeComponent 用作子组件时在 Home 组件中我得到错误 app-create-employee is not a known element 这是预期的,因为 app.module.ts 不知道 CreateEmployeeComponent 在我的 EmployeeModule 的声明数组中声明。如果我不使用延迟加载,我会在 app.module.ts 文件中插入 EmployeeModule,错误将得到修复。我现在无法在我的 app.module.ts 中插入 EmployeeModule,因为这样他就不会被延迟加载。

How can i solve this problem?我怎么解决这个问题?

//app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    PageNotFoundComponent,

  ],
  imports: [
    BrowserModule,
    SharedModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }


// app-routing.module.ts

const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  {path:'employees' , data: {preload:true}, loadChildren:'./modules/employee/employee.module#EmployeeModule'},
  { path: '**', component: PageNotFoundComponent },
];


//employee-routing.module.ts

const routes: Routes = [

    { path: '', component: ListEmployeesComponent },
    { path: 'create', component: CreateEmployeeComponent },
    { path: 'edit/:id', component: CreateEmployeeComponent },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class EmployeeRoutingModule { }


//employee.module.ts

@NgModule({
  imports: [
    EmployeeRoutingModule,
    SharedModule
  ],
  declarations: [
    CreateEmployeeComponent,
    ListEmployeesComponent,
    CompoWithChildrensComponent,
    ChildrenOneComponent,
    ChildrenTwoComponent
  ],
  exports:[
    CreateEmployeeComponent
  ]
})
export class EmployeeModule { }




Because AppModule uses a component from EmployeeModule , you have to import EmployeeModule .因为AppModule使用来自EmployeeModule的组件,所以您必须导入EmployeeModule

You can split EmployeeModule in two modules;您可以将EmployeeModule拆分为两个模块; one module with CreateEmployeeComponent and one module with the rest.一个带有CreateEmployeeComponent的模块和一个带有 rest 的模块。 You can than import the first module and lazy load the second.您可以导入第一个模块并延迟加载第二个模块。

You have to determine for yourself if this is practical and useful for your app.您必须自己确定这对您的应用程序是否实用且有用。

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

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