我爱编程

NgModule in Angular

2018-03-12  本文已影响35人  yingjieg

Why NgModule

It’s done automatically with Angular CLI, but the first thing you have to do in Angular is to load a root NgModule :

The purpose of a NgModule is to declare each thing you create in Angular, and group them together (like Java packages or PHP / C# namespaces).
There is two kind of main structures:

NgModule and scopes / visibility

The confusion starts with declarations and providers not having the same scope / visibility:

It means the components you declared are only usable in the current module. If you need to use them outside, in other modules, you’ll have to export them:

On the contrary, services you provided will generally be available / injectable anywhere in your app, in all modules.

When to import a NgModule ?

The difference of scope between components and services is an important point to know, but for now it’s still OK. Things get messy because, of course, as in any framework and app, you won’t just have one module, but many of them. Angular itself is subdivided in different modules (core, common, http and so on).

So another main thing you do in an Angular module is to import other NgModules you need:

Problem is: you need to know why you import these other modules.

Why ? Because given the difference of scope between components and services:

If you fail to understand this, you’ll have errors on components not being available, because you forgot to import their module again.

Or if you import a module for services more than once, it can lead to errors in advanced scenarios like lazy-loading.

When to import main Angular modules ?

A good knowledge of Angular modules is then required, to know how many times you need to import them. Here is an helpful summary.

Modules to import each time you need them

Modules to import only once

That’s why with Angular CLI, CommonModule is automatically imported when you create a new module.

Mixed NgModules

It can get messier : how to manage modules with components and services at the same time ?

You know one of them : the RouterModule. It gives you a component (<router-outlet>) and a directive (routerLink), but also services (ActivatedRoute to get URL params, Router to navigate…).

Fortunately, the mess is managed by the module itself. Routing files are automatically generated by Angular CLI, but you may have noticed there is a subtle difference between the routing of your first app module and the routing of submodules.

Lazy-loaded modules

Last complication : if you lazy-load a module, which is now easy with Angular CLI.

As it will be a different bundle and module, loaded only on demand by default, it’s not included in the global scope your app.

For components, it doesn’t change anything: you need to import again the CommonModule and other modules of components, like in any submodule.

For services, there is a difference :

Why ?

Now you know everything about Angular modules, you may ask : why this mess ? Well, it may be difficult for beginners, but there is a good reason :

Summary

Your tree of imports(which is not exactly the same as your directories) should look like this :

Architecture in Angular projects

Want to know how to structure your own module in your Angular app ? See the following post.

Original Post URL

上一篇下一篇

猜你喜欢

热点阅读