Skip to content

Commit 03ba961

Browse files
committed
Removed displayContent function usage
1 parent 908e184 commit 03ba961

5 files changed

Lines changed: 24 additions & 32 deletions

File tree

devon4ng-lazy-loading/files/eager-loading.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

devon4ng-lazy-loading/files/intro.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

devon4ng-lazy-loading/files/lazy-loading.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

devon4ng-lazy-loading/files/lazy-loading2.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

devon4ng-lazy-loading/index.asciidoc

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ In this tutorial you will learn how to:
1313
* implement lazy loading and understand its concept and advantages
1414
====
1515

16-
[step]
17-
--
18-
addSetupScript("files/setup.sh")
19-
--
20-
2116
[step]
2217
--
2318
restoreDevonfwIde(["npm", "ng", "vscode"])
2419
--
2520

26-
[step]
27-
--
28-
displayContent("Introduction", [{ "file": "files/intro.txt" }, { "image": "images/levels-app.png" }])
29-
--
21+
To explain how lazy loading is implemented using angular, a basic sample app is going to be developed. This app will consist in a window named "level 1" that contains two buttons that redirects to other windows in a "second level". It is a simple example, but useful to understand the relation between angular modules and lazy loading.
22+
23+
image::images/levels-app.png
24+
25+
This graphic shows that modules acts as gates to access components "inside" them.
26+
27+
Because the objective of this guide is related mainly with logic, the html structure and scss styles are less relevant.
3028

3129
Before creating a devon4ng application, you first have to install the devonfw ide. You will find more information about devonfw on https://devonfw.com/website/pages/welcome/welcome.html.
3230
Once you have setup devonfw ide, you are ready to create your devon4ng application. (Please wait until the devon setup completes and the command prompt appears in the integrated terminal here).
3331
[step]
32+
== Introduction
3433
--
3534
createDevon4ngProject("level-app", "", ["--style=scss", "--routing=true", "--strict=false"])
3635
--
@@ -77,26 +76,27 @@ changeFile("level-app/src/app/first/first.module.ts", { "file": "files/first.mod
7776
runClientNg("level-app", { "startupTime": 200, "port": 4200, "path": "" })
7877
--
7978

80-
[step]
81-
--
82-
displayContent("Eager loading", [{ "file": "files/eager-loading.txt" }, { "image": "images/compile-eager.png" }, { "image": "images/second-lvl-right-eager.png" }])
83-
--
79+
If you run the project at this point you can see in the terminal that just the main file is built.
80+
image::images/compile-eager.png
81+
82+
Go to port 4200 and check the Network tab in the Developer Tools. We can see a document named "first" is loaded. If you click on [Go to right module] a second level module opens, but there is no 'second-right' document.
83+
image::images/second-lvl-right-eager.png
8484

85-
Modifying an angular application to load its modules lazily is easy, you have to change the routing configuration of the desired module (for example `FirstModule`). Instead of loading a component, you dynamically import it in a `loadChildren` attribute because modules acts as gates to access components "inside" them. Updating this app to load lazily has four consecuences: no component attribute, no import of `FirstComponent`, `FirstModule` import has to be removed from the imports array at `app.module.ts`, and change of context.
85+
Now we will modify the app to lazily load the modules. Modifying an angular application to load its modules lazily is easy, you have to change the routing configuration of the desired module (for example `FirstModule`). Instead of loading a component, you dynamically import it in a `loadChildren` attribute because modules acts as gates to access components "inside" them. Updating this app to load lazily has four consecuences: no component attribute, no import of `FirstComponent`, `FirstModule` import has to be removed from the imports array at `app.module.ts`, and change of context.
8686

8787
Also, in `first-routing.module.ts` you can change the path for the `ContentComponent`s from `first/second-left` and `first/second-right` to simply `second-left` and `second-right` respectively, because it aquires the context set by AppRoutingModule.
8888
[step]
89-
== Transitioning to lazy loading
89+
== Eager loading vs Lazy Loading
9090
--
9191
changeFile("level-app/src/app/app-routing.module.ts", { "file": "files/app-routing.module.update2.ts.txt" })
9292
changeFile("level-app/src/app/app.module.ts", { "file": "files/app.module.ts.update2.txt" })
9393
changeFile("level-app/src/app/first/first-routing.module.ts", { "file": "files/first-routing.module.update2.ts.txt" })
9494
--
9595

96-
[step]
97-
--
98-
displayContent("Lazy loading First module", [{ "file": "files/lazy-loading.txt" }, { "image": "images/compile-first-lazy.png" }, { "image": "images/first-lvl-lazy.png" }])
99-
--
96+
====
97+
Now when you check the terminal running the app, you could see the lazy loaded modules getting generated along with the main bundle. Also, if you check the Network tab in the developer tools, you could see the (lazy) modules getting loaded when needed. Since, `FirstModule` is the first path we visit, it is getting loaded at first only.
98+
image::images/compile-first-lazy.png
99+
image::images/first-lvl-lazy.png
100100
101101
Now, lets make the SecondLeftModule load lazily. For this, you need to change `component` to `loadChildren` and refer `SecondLeftModule` in the file `first-routing.module.ts`. Next, you need to remove `SecondLeftModule` from the `imports` array of `first.module.ts`. After that you need to route the `ContentComponent` within the `second-left-routing.module.ts`.
102102
[step]
@@ -106,11 +106,12 @@ changeFile("level-app/src/app/first/first-routing.module.ts", { "file": "files/f
106106
changeFile("level-app/src/app/first/first.module.ts", { "file": "files/first.module.update3.ts.txt" })
107107
changeFile("level-app/src/app/first/second-left/second-left-routing.module.ts", { "file": "files/second-left-routing.module.update3.ts.txt" })
108108
--
109+
If you now check the terminal, you could also see `second-left-second-left-module` along with the `first-first-module` and the `main` bundle getting generated.
110+
image::images/second-lvl-lazy.png
109111
110-
[step]
111-
--
112-
displayContent("Lazy loading SecondLeftModule", [{ "file": "files/lazy-loading2.txt" }, { "image": "images/second-lvl-lazy.png" }, { "image": "images/second-lvl-left-lazy.png" }])
113-
--
112+
Also, in the Network tab of the developer tools, you could see the `second-left-second-left-module.js` is only loading when we click on the [Go to left module] button
113+
image::images/second-lvl-left-lazy.png
114+
====
114115

115116
====
116117
Lazy loading is a pattern useful when new features are added, these features are usually identified as modules which can be loaded only if needed as shown in this tutorial, reducing the time spent loading an application.

0 commit comments

Comments
 (0)