Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Lazy loading does not work when integrating into another application

Tags:

angular

I want to use lazy-loading for feature modules. I use angular-cli and it works fine when I run ng serve. Now I'm trying to include the js files generated by angular-cli in a legacy jquery application :

  • Copy 0.chunk.js, inline.bundle.js, main.bundle.js, polyfills.bundle.js, styles.bundle.js and vendor.bundle.js in a directory called angular
  • In my html file, I'm loading the files :

    /js/angular/inline.bundle.js /js/angular/polyfills.bundle.js /js/angular/styles.bundle.js /js/angular/vendor.bundle.js /js/angular/main.bundle.js

In my application, let's say I go to this webpage : http://localhost:8090/myapp/test.action

In order to integrate angular components in my application, I'm using the router to redirect to the component I want to display in the AppComponent :

ngOnInit(): void {
   if (this.displayComponent) {
      this.router.navigate([this.displayComponent]);
   }
}

Let's say I want to display my component called 'componentTest', it will redirect to : http://localhost:8090/myapp/test.action#/componentTest with this code in my html file :

<app-root displayComponent="componentTest">Loading...</app-root>

And then I have this error :

EXCEPTION: Uncaught (in promise): Error: Loading chunk 0 failed.

It seems is is trying to load the chunk file at this address :

http://localhost:8090/myapp/0.chunk.js

and not

http://localhost:8090/js/angular/0.chunk.js

How to fix this issue ?

Thanks

This is working perfectly fine without lazy loading.

Windows 10

Angular version:
@angular/common: 2.4.8
@angular/compiler: 2.4.8
@angular/core: 2.4.8
@angular/forms: 2.4.8
@angular/http: 2.4.8
@angular/platform-browser: 2.4.8
@angular/platform-browser-dynamic: 2.4.8
@angular/router: 3.4.8
@angular/material: 2.0.0-beta.2
@angular/cli: 1.0.0-rc.0
@angular/compiler-cli: 2.4.8

Chrome 56

Typescript 2.0

node --version = v6.9.5
like image 775
shaun_abitbol Avatar asked Mar 02 '17 14:03

shaun_abitbol


People also ask

What are the disadvantages of lazy loading in Angular?

Disadvantages of Lazy loading Firstly, the extra lines of code, to be added to the existing ones, to implement lazy load makes the code a bit complicated. Secondly, lazy loading may affect the website's ranking on search engines sometimes, due to improper indexing of the unloaded content.

How do I know if Angular lazy loading is working?

If you want to check how lazy loading works and how lazy loading routing flow, then Augury is the best tool we have. Click on ctrl+F12 to enable the debugger and click on the Augury tab. Click on the router tree. Here, it will show the route flow of our modules.

How do you do lazy loading when should you use lazy loading in Angular?

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.


1 Answers

The folder to load chunk files is actually configurable in angular-cli. I added the "--deploy-url /js/angular" flag to the ng build command.

https://www.reddit.com/r/Angular2/comments/5wozkl/angularcli_how_can_i_allow_content_to_be_served/

like image 173
shaun_abitbol Avatar answered Nov 15 '22 04:11

shaun_abitbol