-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazy loading in Angular2 RC7 and angular-cli webpack #2112
Comments
Try killing and restarting ng serve. Worked for me, see #2033 |
yep it works, gj! |
@slubowsky @mjolk is there any way i can change the name of generated chunks that you know ? my chunks are named |
@ritz078 normally this is easy to do in webpack config but in this case this config is entirely managed by angular-cli... if needs must you could edit https://github.com/angular/angular-cli/blob/4c4e31d44c7b17078ebac6f4689db11c47285661/packages/angular-cli/models/webpack-build-development.ts#L10 and https://github.com/angular/angular-cli/blob/4c4e31d44c7b17078ebac6f4689db11c47285661/packages/angular-cli/models/webpack-build-production.ts#L14 |
that's definitely not a solution. will create a separate issue. |
Nothing has worked for me. Killing and restarting ng serve has no effect. I've seen another post exactly the same as this on stack overflow so I'm not the only one getting this. One person has suggested the below: "The mechanism you are using is still systemjs based. I couldn't make it work with webpack using the "path#class" syntax. So we require the module directly: First we'll need the es6-promise-loader: npm i --save-dev es6-promise-loader Then define your route like this: {path:"lazy", loadChildren: () => require('es6-promise!./path/to/module')('ClassName')}" I'm not sure about this though. I would have thought the lazy loading that used to work with System JS should work the same in webpack with no extra config. Am I wrong? |
I just got it working. Restarting |
Isn't it possible to lazy load just a Component instead of a whole NgModule? |
With the latest beta.14 and Angular 2.0.0 Final I used |
Maybe it's better to follow the documentation style:
|
@chosroes That's how I was originally doing it but it could never find the module file. Removing the |
One thing that failed me at first: make sure that the class inside your module is NOT exported as 'default', otherwise it will not be picked up by systemJS loader! |
@reflog Thanks for pointing that out. After removing |
@andyrue Yeah, default means that you have not to specify the module. |
Are you guys seeing it actually create additional bundles? Cuz although this appears to be working, in that I'm not getting any errors, it doesn't appear to be creating separate bundles. Therefore, nothing is really being lazy loaded when you look at the network tab. UPDATE: Never mind, I had to remove the module from the main app.module.ts and now it loads as a seperate chunk. I was surprised this worked, lol. |
I'm having the same issue mostly because the children aren't relative to the the project. I have it in another package. Is there anyway to have loadChildren work like import somehow? import { CoreModule } from '@blahblah/core-stuff'; |
Lazy loading Worked for me
|
does anybody have a working plnkr example? I'm using angular-cli and can't figure it out |
Lazy loading seems to be working for me. |
@stephenwilson1984 are you still having problems? If you show me repo with the latest CLI, I can try to debug. |
Hi, Everything is fine now, we ditched angular-cli and stayed using SystemJs which works fine for now. Thanks, |
@filipesilva would using the same module in the root app and a lazy loaded module cause this? For example. I have a core module. In the root app I call forRoot to configure a provider. Then when I import that same module in a lazy loaded module I do not get multiple bundles |
@filipesilva I think the issue is that it is not obvious how the cli decides to create bundles since it seems it is not just defined by defining a module to be lazy loaded. |
Load children in webpack try to use SystemJS loader for resolve. But I don't want to use it. |
@tianyangj
but |
I have same issue here. using webpack without angular cli worked fine, but with cli it does not generate bundles |
I was having the same issue as @Delagen. I was defining routes in this format from https://angular.io/docs/ts/latest/guide/router.html.
Updating to the module format used in the angular-cli blueprints works fixes this error
Once you get past this issue you hit #2452 noted above. |
@onedotover No I issue different problem |
@filipesilva I encountered a lazy loading issue but it is unlike stephenwilson1984's. Instead of 'Cannot find module', the main bundle includes the lazily loaded module. Per your request I have a repo that reproduces the issue, under this related issue: |
same for me, everything is packed into single module, instead having multiple generated bundles. |
angular-cli beta 17 the same issue. |
For issues related to AOT and lazy loaded modules, please use #2452 instead of this issue. We have a custom mechanism in the CLI to find and separately bundle lazy loaded routes that does not need SystemJS and instead uses Webpack2. Regarding bundles not being generated. the cause is likely that you are importing that module elsewhere in the app, as mentioned in #2112 (comment). As long as there is at least one ES6 I'm closing this issue because the exact reported issue doesn't exist anymore, it was originally for an older version, the original poster isn't part of the discussion anymore, and the thread is branching out to related but different issues. Please open new issues for problems you're still having with lazy loaded modules. |
I confirm that the restarting of |
"restarting ng serve" worked for me too |
still not working for me... How does cli "automatically understand and find lazy routes"? Maybe I do something wrong that this automatic code cant find any matches in my project? I have export const APP_ROUTES: Routes = [
{
path: "posts",
loadChildren: "../modules/content-modules/post/PostModule#PostModule"
},
]; |
make sure that you included 'Routes' in the lazy module as described below: |
Felt this bug for the first time today. No issues locally (dev or production / Mac OS), but got hit with it after I pushed to my production server (Heroku / Ubuntu). |
I just put the route in the app.module.ts file, if I add a new file called route.module.ts and try to import within the app.module.ts file, the app will crash, so if you just leave the code inline, it should work. import { Routes, RouterModule } from '@angular/router';
...
const routes: Routes = [
{ path: '', loadChildren: './home/home.module#HomeModule' },
{ path: 'contacts', component: ContactsComponent }
];
const routerModule = RouterModule.forRoot(routes);
@NgModule({
declarations: [
AppComponent,
ContactsComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routerModule
],
providers: [],
bootstrap: [AppComponent]
}) References: |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?)
Windows 7
Versions. Please run
ng --version
. If there's nothing outputted, please runin a Terminal:
node --version
and paste the result here:angular-cli: 1.0.0-beta.11-webpack.9-4
node: 6.5.0
os: win32 x64
Repro steps. Was this an app that wasn't created using the CLI? What change did you
do on your code? etc.
Created a boilerplate app using "ng new" then added a basic home module manually.
The log given by the failure. Normally this include a stack trace and some
more information.
error_handler.js:45EXCEPTION: Uncaught (in promise): Error: Cannot find module '/app/home.module'.ErrorHandler.handleError @ error_handler.js:45next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:196Zone.runGuarded @ zone.js:102_loop_1 @ zone.js:368drainMicroTaskQueue @ zone.js:375
error_handler.js:50ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:196Zone.runGuarded @ zone.js:102_loop_1 @ zone.js:368drainMicroTaskQueue @ zone.js:375
error_handler.js:51Error: Uncaught (in promise): Error: Cannot find module '/app/home.module'.
at resolvePromise (zone.js:418)
at zone.js:395
at ZoneDelegate.invoke (zone.js:192)
at Object.onInvoke (ng_zone_impl.js:43)
at ZoneDelegate.invoke (zone.js:191)
at Zone.run (zone.js:85)
at zone.js:451
at ZoneDelegate.invokeTask (zone.js:225)
at Object.onInvokeTask (ng_zone_impl.js:34)
at ZoneDelegate.invokeTask (zone.js:224)ErrorHandler.handleError @ error_handler.js:51next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:196Zone.runGuarded @ zone.js:102_loop_1 @ zone.js:368drainMicroTaskQueue @ zone.js:375
zone.js:344Unhandled Promise rejection: Cannot find module '/app/home.module'. ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot find module '/app/home.module'.(…) Error: Cannot find module '/app/home.module'.
at webpackEmptyContext (http://localhost:4200/main.bundle.js:41545:8)
at SystemJsNgModuleLoader.loadAndCompile (http://localhost:4200/main.bundle.js:50064:40)
at SystemJsNgModuleLoader.load (http://localhost:4200/main.bundle.js:50057:60)
at RouterConfigLoader.loadModuleFactory (http://localhost:4200/main.bundle.js:16894:128)
at RouterConfigLoader.load (http://localhost:4200/main.bundle.js:16886:81)
at MergeMapSubscriber.project (http://localhost:4200/main.bundle.js:52671:111)
at MergeMapSubscriber._tryNext (http://localhost:4200/main.bundle.js:25798:27)
at MergeMapSubscriber._next (http://localhost:4200/main.bundle.js:25788:18)
at MergeMapSubscriber.Subscriber.next (http://localhost:4200/main.bundle.js:5728:18)
at ScalarObservable._subscribe (http://localhost:4200/main.bundle.js:40540:24)consoleError @ zone.js:344_loop_1 @ zone.js:371drainMicroTaskQueue @ zone.js:375
zone.js:346Error: Uncaught (in promise): Error: Cannot find module '/app/home.module'.(…)
Mention any other details that might be useful.
Also a question raised on StackOverflow with more details: http://stackoverflow.com/q/39493782/616304
The text was updated successfully, but these errors were encountered: