loadChildren is deprecated in angular8

9.2k Views Asked by At

I just noticed that Angular 8 (which will be released soon), has deprecated "string-typed router loadChildren". (ticket)

Do I understand correctly that they are refering to ...

const routes = [
  {
    path: 'production',
    loadChildren: './production/production.module#ProductionModule' // <<--this
  }],

To migrate to Angular 8, what is the solution ?

In the ticket they are refering to "dynamic imports". Am I correct that this refers to the following proposal:

let module = await import('/modules/my-module.js');

Could somebody give a preview of what the routing file should actually look like if we wanted to use lazy loading in the future ?

2

There are 2 best solutions below

0
On

As loadChildren:string is deprecated in angular 8 Change your loadChildren declarations

From

loadChildren: './production/production.module#ProductionModule'

To

loadChildren: () => import('./production/production.module').then(m => m.ProductionModule)

For more you can ref angular official git link and commit

3
On

Apparently, it's not the entire "loadChildren" : ... which is deprecated. It just will no longer accept strings. Instead you have to specify a function now.

The documentation is already available here.

It comes down to:

const routes = [
  {
    path: 'lazy',
    loadChildren : () => import('./production/production.module').then(m => m.ProductionModule),
  }
];