How do you lazy load one component in Angular2 (RC5)

I managed to get lazy loading to work with NgModules using the prescribed loadChildren Route attribute.

import {RouterModule} from ‘@angular/router’
import {NgModule} from ‘@angular/core’
@NgModule({
  declarations: [ MyComponent, MyHomeRoute ],
  bootstrap: [ MyComponent ],
  imports: [
     RouterModule.forRoot([
     { path: ‘home’, component: MyHomeRoute },
     { path: ‘lazy’, loadChildren: ‘./my-lazy-module’ }
  ])
})
class MyAppModule {}

But is there a way to configure Route to lazily load a single component (unlike NgModule) using the string for the "component" attribute?

I always get errors like:
"Unable to resolve component using" lazyComponent "

+4
source share
1 answer
@NgModule({
  imports:      [ BrowserModule, routing ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Then download on demand:

import { Routes, RouterModule } from '@angular/router'; 
 const routes: Routes= [{path: 'welcome', loadChildren: 'app/screens/welcome.module'}];
export const routing = RouterModule.forRoot(routes);

here: loadChildren: 'app / screens / welcome.module' is the ts file app / screens / welcome.module.ts

, : http://plnkr.co/edit/18CZVF?p=preview

0

Source: https://habr.com/ru/post/1651334/


All Articles