简体   繁体   English

Angular 4应用程序内部的延迟加载

[英]Lazy Loading inside Angular 4 application

I am trying to implement Lazy Loading into my application but am coming across an issue which I have been stuck on for a couple of hours. 我正在尝试在我的应用程序中实现延迟加载,但是遇到了一个问题,我已经坚持了几个小时。

I have my main app.module.ts file: 我有主要的app.module.ts文件:

import { AppComponent } from './app.component';
import { OverviewComponent } from './application/overview/overview.component';

@NgModule({
  declarations: [
    AppComponent,
    OverviewComponent,
  ],
  imports: [
    RoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

I also have a routing.module.ts file: 我也有一个routing.module.ts文件:

const appRoutes: Routes = [
  { path: '', redirectTo: 'overview', pathMatch: 'full' },
  { path: 'overview', component: OverviewComponent },
  { path: 'search', loadChildren: '../../application/search/search.module#SearchModule' },  
  { path: 'policy', loadChildren: '../../application/policy/policy.module#PolicyModule' },
  { path: 'claim', loadChildren: '../../application/claim/claim.module#ClaimModule' }
];

Then I want to Lazy Load the search.module.ts module in, to which the search.module.ts looks like the following: 然后,我要延迟加载search.module.ts模块,其中search.module.ts如下所示:

import { SearchRoutingModule } from './search-routing.module';
import { MaterialModule } from '../../configuration/material/material.module';
import { SharedModule } from '../../application/shared/shared.module';
import { SearchComponent } from '../search/search.component';

@NgModule({
  imports: [
    CommonModule,
    MaterialModule,
    SharedModule,
    SearchRoutingModule
  ],
  declarations: [SearchComponent]
})
export class SearchModule { }

Finally, we have the search-routing.module.ts 最后,我们有search-routing.module.ts

import { SearchComponent } from '../search/search.component'  

const routes: Routes = [
  { path: '', component: SearchComponent },
];

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

export class SearchRoutingModule { }

For some reason the application loads up correctly and displays the Overview component fine. 由于某些原因,应用程序可以正确加载并正常显示Overview组件。 As soon as I navigate to /search I get an error saying: 一旦导航到/ search,我就会收到一条错误消息:

Component OverviewComponent is not part of any NgModule or the module has not been imported into your module. 组件概述组件不是任何NgModule的一部分,或者模块尚未导入到您的模块中。 Error: Component OverviewComponent is not part of any NgModule or the module has not been imported into your module. 错误:组件概述组件不是任何NgModule的一部分,或者模块尚未导入到您的模块中。

Does anyone have an idea to why this is happening. 有谁知道为什么会这样。

If you need to use the OverviewComponent somewere in your SearchModule , then you need to import the module in which HomeComponent is declared into your SearchModule . 如果您需要使用OverviewComponent在somewere SearchModule ,那么你需要导入该模块HomeComponent被宣布为您SearchModule

If, here HomeComponent is declared in the main AppModule ; 如果,这里HomeComponent在主宣布AppModule ; maybe you should move OverviewComponent into a shared module and import that shared module into both AppModule and SearchModule 也许你应该移到OverviewComponent成共享模块到两个共享的模块和进口AppModuleSearchModule

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

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