I have created a new app using ng new
but when I try to configure a module to load using lazy loading I keep getting an error that the module cannot be found.
EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'
"dependencies": {
"@angular/common": "2.0.0-rc.7",
"@angular/compiler": "2.0.0-rc.7",
"@angular/core": "2.0.0-rc.7",
"@angular/forms": "2.0.0-rc.7",
"@angular/http": "2.0.0-rc.7",
"@angular/platform-browser": "2.0.0-rc.7",
"@angular/platform-browser-dynamic": "2.0.0-rc.7",
"@angular/router": "3.0.0-rc.3",
"core-js": "^2.4.1",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.21"
},
"devDependencies": {
"@types/jasmine": "^2.2.30",
"angular-cli": "1.0.0-beta.11-webpack.9-4",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.5",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const appRoutes: Routes = [{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: `home`,
loadChildren: `app/home/home.module`
}];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
title = 'app works!';
}
<router-outlet></router-outlet>
I'm sure I'm missing something stupid but I tried various options and nothing has worked so far. Given my configuration, am I missing anything?
Thanks in advance, Stephen
Lazy loading helps us to download the web pages in chunks instead of downloading everything in a big bundle. To implement the Lazy Loading in Angular we need to create a routing module and a module. ts file for the component we need to lazy load. In the above image, we have created two files which are posts-routing.
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items.module').then(m => m.ItemsModule) } ];
Lazy, or "on demand", loading is a great way to optimize your site or application. This practice essentially involves splitting your code at logical breakpoints, and then loading it once the user has done something that requires, or will require, a new block of code.
Therefore, to make angular understand the need to load BackendService , the new steps are: lazy load the module, compile it to notify Angular about its dependencies, finally, through the compiled module, we access the component and then add it to the container.
With RC7, you can do as follow:
const appRoutes: Routes = [
{
path: 'home',
loadChildren: 'app/home/home.module#HomeModule',
}
];
Don't forget the "#"
Then kill ng serve and restart it
It will works with angular-cli@webpack
P.S: The angular convention is to prefix lazy loaded folders with a (+) e.g. +home/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With