简体   繁体   English

整个应用程序的ngModule导入模块?

[英]ngModule import module for entire app?

I'm upgrading my app to angular2 RC and I have a question about ngModule. 我正在将我的应用程序升级到angular2 RC,我对ngModule有疑问。

Lets say I use some componentsModule that has all kind of components that I want to use around my app. 让我说我使用一些componentsModule,它包含我想在我的应用程序中使用的所有类型的组件。

Why would I need to import them in every module separately? 为什么我需要分别在每个模块中导入它们? Why cant I just tell the appModule - "hi make sure everyone gets this." 为什么我不能告诉appModule - “嗨,确保每个人都能得到这个。”

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 ? 或者angular2使用它们作为单身人士? 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. 模块只是Angular编译器告诉的一种方式,它在通过模板时应该期望哪些组件。

Ideally, you want your AppModule to be clean and only import what it needs to bootstrap the page. 理想情况下,您希望AppModule干净并只导入引导页面所需的内容。 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. 如果您经常在许多不同的模板中有一个组件,则可以创建一个SharedModule,它将声明应用程序中最常见的组件和管道,并在功能模块中导入该共享模块。

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. 当Angular 2编译您的要素模块并从共享模块中找到一个组件时,它将知道在您导入该模块后如何编译它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ionic 4 在 app.module.ts 页面中出现错误,从 '@angular/core' 导入 { NgModule }; - ionic 4 getting an error in app.module.ts page, import { NgModule } from '@angular/core'; 在 angular 2 应用程序的 NgModule 导入部分中注入依赖项 - Injecting dependencies inside the NgModule import section of an angular 2 app 在ngModule导入中找不到模块 - Module not found in ngModule imports 错误:找不到“模块”的NgModule元数据 - Error: No NgModule metadata found for 'module' 在每个模块上导入 NgModule 和 FormModule? - NgModule and FormModule importing on every module? 找不到 [object Module] 的 ngmodule 元数据 - no ngmodule metadata found for [object Module] 我的库模块无法识别为NgModule - My library module is not recognize as an NgModule 我们为什么不在app.module.ts的装饰器数组中添加“ ngModule” - Why dont we add 'ngModule' in decorator array of app.module.ts 是否有可能(以及如何)在另一个类的app.module中访问NgModule中的声明数组? - Is it possible (and how) to access the declarations array inside NgModule in app.module in another class? 将Angular4模块链接到Angular4应用程序:模块'AppModule'导入了意外的值'TestModule'。 请添加@NgModule批注 - Linking Angular4 Module to Angular4 app: Unexpected value 'TestModule' imported by the module 'AppModule'. Please add a @NgModule annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM