NgModule中的声明,提供程序和导入之间有什么区别? [英] What is the difference between declarations, providers, and import in NgModule?

查看:191
本文介绍了NgModule中的声明,提供程序和导入之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Angular(有时称为Angular2 +),然后碰到了@Module:

I am trying to understand Angular (sometimes called Angular2+), then I came across @Module:

  1. 导入

  1. Imports

声明

提供商

遵循 Angular快速入门

推荐答案

角度概念

  • imports使其他模块的导出声明在当前模块中可用
  • declarations用于使当前模块中的指令(包括组件和管道)可用于当前模块中的其他指令.指令,组件或管道的选择器只有在声明或导入时才与HTML匹配.
  • providers将使DI知道服务和值(依赖注入).它们被添加到根范围,并被注入到其他具有它们依赖关系的服务或指令中.
  • imports makes the exported declarations of other modules available in the current module
  • declarations are to make directives (including components and pipes) from the current module available to other directives in the current module. Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported.
  • providers are to make services and values known to DI (dependency injection). They are added to the root scope and they are injected to other services or directives that have them as dependency.

providers的一种特殊情况是延迟加载的模块,它们具有自己的子注入器.默认情况下,延迟加载模块的providers仅提供给该延迟加载模块(不像其他模块那样提供整个应用程序).

A special case for providers are lazy loaded modules that get their own child injector. providers of a lazy loaded module are only provided to this lazy loaded module by default (not the whole application as it is with other modules).

有关模块的更多详细信息,另请参见 https://angular.io/docs/ts/latest/guide/ngmodule.html

For more details about modules see also https://angular.io/docs/ts/latest/guide/ngmodule.html

  • exports使组件,指令和管道在将此模块添加到imports的模块中可用. exports也可以用于重新导出CommonModule和FormsModule之类的模块,这通常是在共享模块中完成的.

  • exports makes the components, directives, and pipes available in modules that add this module to imports. exports can also be used to re-export modules such as CommonModule and FormsModule, which is often done in shared modules.

entryComponents注册用于脱机编译的组件,以便它们可以与ViewContainerRef.createComponent()一起使用.路由器配置中使用的组件是隐式添加的.

entryComponents registers components for offline compilation so that they can be used with ViewContainerRef.createComponent(). Components used in router configurations are added implicitly.

TypeScript(ES2015)导入

import ... from 'foo/bar'(其中可以解析为index.ts )用于TypeScript导入.每当在另一个打字稿文件中声明的打字稿文件中使用标识符时,都需要这些.

import ... from 'foo/bar' (which may resolve to an index.ts) are for TypeScript imports. You need these whenever you use an identifier in a typescript file that is declared in another typescript file.

Angular的@NgModule() imports和TypeScript import完全不同的概念.

Angular's @NgModule() imports and TypeScript import are entirely different concepts.

另请参见 jDriven-TypeScript和ES6导入语法

其中大多数实际上是TypeScript也使用的普通ECMAScript 2015(ES6)模块语法.

Most of them are actually plain ECMAScript 2015 (ES6) module syntax that TypeScript uses as well.

这篇关于NgModule中的声明,提供程序和导入之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆