简体   繁体   中英

ngModule import module for entire app?

I'm upgrading my app to angular2 RC and I have a question about ngModule.

Lets say I use some componentsModule that has all kind of components that I want to use around my app.

Why would I need to import them in every module separately? Why cant I just tell the appModule - "hi make sure everyone gets this."

Wont requiring that module over and over again in each module that uses him is just a waste of resources ? Or is angular2 uses them as singletons ? and if so cant that be a problem ? - What If I do need new insentces

Modules are just a way for Angular's compiler to tell, which components it should expect when going through the templates.

Ideally, you want your AppModule to be clean and only import what it needs to bootstrap the page. Next, you import (or route to lazy-loaded modules) your feature modules, which make up your application.

In case you often have one component in many different templates, you can create a SharedModule, which will declare the most common components and pipes in your application, and import that shared module in your feature modules.

When Angular 2 compiles your feature module and finds a component from a shared module, it will know how to compile it since you imported that module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM