2017-07-04 10:58:20 -04:00
# JavaScript Modules vs. NgModules
2018-03-20 03:40:07 -04:00
# JavaScript 模块 vs. NgModule
2017-07-04 10:58:20 -04:00
#### Prerequisites
2018-03-03 08:06:01 -05:00
2018-03-17 19:26:14 -04:00
#### 前提条件
2018-02-19 10:35:13 -05:00
A basic understanding of [JavaScript/ECMAScript modules ](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ).
2017-07-04 10:58:20 -04:00
2018-03-20 03:40:07 -04:00
对 [JavaScript/ECMAScript 模块 ](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ) 有基本的了解。
2017-07-04 10:58:20 -04:00
< hr >
JavaScript and Angular use modules to organize code, and
though they organize it differently, Angular apps rely on both.
2018-03-20 03:40:07 -04:00
JavaScript 和 Angular 都使用模块来组织代码,虽然它们的组织形式不同,但 Angular 的应用会同时依赖两者。
2017-07-04 10:58:20 -04:00
## JavaScript modules
2018-03-20 03:40:07 -04:00
## JavaScript 模块
2017-07-04 10:58:20 -04:00
In JavaScript, modules are individual files with JavaScript code in them. To make what’ s in them available, you write an export statement, usually after the relevant code, like this:
2018-03-20 03:40:07 -04:00
在 JavaScript 中,模块是内含 JavaScript 代码的独立文件。要让其中的东西可用,你要写一个导出语句,通常会放在相应的代码之后,类似这样:
2018-01-17 07:57:43 -05:00
```typescript
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
export class AppComponent { ... }
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
```
Then, when you need that file’ s code in another file, you import it like this:
2018-03-20 03:40:07 -04:00
然后,当你在其它文件中需要这个文件的代码时,要像这样导入它:
2018-01-17 07:57:43 -05:00
```typescript
2018-03-06 22:25:56 -05:00
2018-01-17 07:57:43 -05:00
import { AppComponent } from './app.component';
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
```
JavaScript modules help you namespace, preventing accidental global variables.
2018-03-20 03:40:07 -04:00
JavaScript 模块让你能为代码加上命名空间,防止因为全局变量而引起意外。
2017-07-04 10:58:20 -04:00
## NgModules
<!-- KW - - perMisko: let's discuss. This does not answer the question why it is different. Also, last sentence is confusing. -->
2018-03-06 22:25:56 -05:00
2018-01-13 03:47:27 -05:00
NgModules are classes decorated with `@NgModule` . The `@NgModule` decorator’ s `imports` array tells Angular what other NgModules the current module needs. The modules in the `imports` array are different than JavaScript modules because they are NgModules rather than regular JavaScript modules. Classes with an `@NgModule` decorator are by convention kept in their own files, but what makes them an `NgModule` isn’ t being in their own file, like JavaScript modules; it’ s the presence of `@NgModule` and its metadata.
2017-07-04 10:58:20 -04:00
2018-03-20 03:40:07 -04:00
NgModule 是一些带有 `@NgModule` 装饰器的类。`@NgModule` 装饰器的 `imports` 数组会告诉 Angular 哪些其它的 NgModule 是当前模块所需的。
`imports` 数组中的这些模块与 JavaScript 模块不同,它们都是 NgModule 而不是常规的 JavaScript 模块。
带有 `@NgModule` 装饰器的类通常会习惯性地放在单独的文件中,但单独的文件并不像 JavaScript 模块那样作为必要条件,而是因为它带有 `@NgModule` 装饰器及其元数据。
2017-07-04 10:58:20 -04:00
The `AppModule` generated from the Angular CLI demonstrates both kinds of modules in action:
2018-03-20 03:40:07 -04:00
Angular CLI 生成的 `AppModule` 实际演示了这两种模块:
2018-01-17 07:57:43 -05:00
```typescript
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
/* These are JavaScript import statements. Angular doesn’ t know anything about these. */
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
/* The @NgModule decorator lets Angular know that this is an NgModule. */
@NgModule ({
2018-01-17 07:57:43 -05:00
declarations: [
AppComponent
],
imports: [ /* These are NgModule imports. */
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
2017-07-04 10:58:20 -04:00
})
export class AppModule { }
2018-03-06 22:25:56 -05:00
2017-07-04 10:58:20 -04:00
```
2018-01-13 03:47:27 -05:00
The NgModule classes differ from JavaScript module in the following key ways:
2017-07-04 10:58:20 -04:00
2018-03-20 03:40:07 -04:00
NgModule 类与 JavaScript 模块有下列关键性的不同:
2017-07-04 10:58:20 -04:00
* An NgModule bounds [declarable classes ](guide/ngmodule-faq#q-declarable ) only.
Declarables are the only classes that matter to the [Angular compiler ](guide/ngmodule-faq#q-angular-compiler ).
2018-03-03 08:06:01 -05:00
2018-03-20 05:09:18 -04:00
Angular 模块只绑定了[_可声明的类_](guide/ngmodule-faq#q-declarable),这些可声明的类只是供[Angular 编译器](guide/ngmodule-faq#q-angular-compiler)用的。
2018-03-07 01:35:57 -05:00
2017-07-04 10:58:20 -04:00
* Instead of defining all member classes in one giant file as in a JavaScript module,
you list the module's classes in the `@NgModule.declarations` list.
2018-03-03 08:06:01 -05:00
2018-03-20 03:40:07 -04:00
与 JavaScript 类把它所有的成员类都放在一个巨型文件中不同,你要把该模块的类列在它的 `@NgModule.declarations` 列表中。
2017-07-04 10:58:20 -04:00
* An NgModule can only export the [declarable classes ](guide/ngmodule-faq#q-declarable )
it owns or imports from other modules. It doesn't declare or export any other kind of class.
2018-03-03 08:06:01 -05:00
2018-03-20 05:09:18 -04:00
Angular 模块只能导出[_可声明的类_](guide/ngmodule-faq#q-declarable)。这可能是它自己拥有的也可能是从其它模块中导入的。它不会声明或导出任何其它类型的类。
2018-03-07 02:42:49 -05:00
2017-07-04 10:58:20 -04:00
* Unlike JavaScript modules, an NgModule can extend the _entire_ application with services
by adding providers to the `@NgModule.providers` list.
2018-03-20 03:40:07 -04:00
与 JavaScript 模块不同, NgModule 可以通过把服务提供商加到 `@NgModule.providers` 列表中,来用服务扩展*整个*应用。
2017-07-04 10:58:20 -04:00
< hr / >
## More on NgModules
2018-03-20 03:40:07 -04:00
## 关于 NgModule 的更多知识
2017-07-04 10:58:20 -04:00
For more information on NgModules, see:
2018-03-03 08:06:01 -05:00
2018-03-20 03:40:07 -04:00
要了解关于 NgModule 的更多知识,参见
2017-07-04 10:58:20 -04:00
* [Bootstrapping ](guide/bootstrapping ).
2018-03-03 08:06:01 -05:00
2018-03-20 03:40:07 -04:00
[引导 ](guide/bootstrapping ).
2017-07-04 10:58:20 -04:00
* [Frequently used modules ](guide/frequent-ngmodules ).
2018-03-03 08:06:01 -05:00
2018-03-20 03:40:07 -04:00
[常用模块 ](guide/frequent-ngmodules ).
2017-07-04 10:58:20 -04:00
* [Providers ](guide/providers ).
2018-03-20 03:40:07 -04:00
[服务提供商 ](guide/providers ).