简体   繁体   中英

Angular Lazy Loading Modules and entryComponents error

I'm lazy loading an angular module and while trying to open my DatesModal I'm getting this error:

No component factory found for DatesModal. Did you add it to @NgModule.entryComponents?

My lazyModule looks like this:

declarations: [DatesModal] entryComponents: [DatesModal]

Im definitely adding the DatesModal in the entryComponents array of the lazyModule. Anyone got any idea what I could be missing here?

Please let me know if you need more info to answer this question.

It looks like you want to get/import this module in other modules, however this module is not visible to others. Try to export your module:

exports: [DatesModal]

UPDATE:

As Angular docs says:

For production apps you want to load the smallest code possible. The code should contain only the classes that you actually need and exclude components that are never used. For this reason, the Angular compiler only generates code for components which are reachable from the entryComponents; This means that adding more references to @NgModule.declarations does not imply that they will necessarily be included in the final bundle. If a component isn't an entry component and isn't found in a template, the tree shaker will throw it away. So, it's best to add only the components that are truly entry components to help keep your app as trim as possible.

To make your module to be lzay loaded, you need to do in routes:

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: () => import('./customers/customers.module').then(mod => mod.CustomersModule)
  },
  {
    path: 'orders',
    loadChildren: () => import('./orders/orders.module').then(mod => mod.OrdersModule)
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];

Read this docs about how to make your modules to be lazy loaded.

Have you used any third-party library (or yourself) to dynamic create this component in lazy-load module?

If so, you need to add this component in the AppModule's (or up-level non-lazy-load module) entrycomponents .

More detail you can see this issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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