angular-cn/aio/content/guide/ngmodule-vs-jsmodule.md

94 lines
3.4 KiB
Markdown
Raw Normal View History

# JavaScript Modules vs. NgModules
#### Prerequisites
2018-03-03 08:06:01 -05:00
#### 前提条件
A basic understanding of [JavaScript/ECMAScript modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/).
<hr>
JavaScript and Angular use modules to organize code, and
though they organize it differently, Angular apps rely on both.
## JavaScript modules
In JavaScript, modules are individual files with JavaScript code in them. To make whats in them available, you write an export statement, usually after the relevant code, like this:
```typescript
2018-03-06 22:25:56 -05:00
export class AppComponent { ... }
2018-03-06 22:25:56 -05:00
```
Then, when you need that files code in another file, you import it like this:
```typescript
2018-03-06 22:25:56 -05:00
import { AppComponent } from './app.component';
2018-03-06 22:25:56 -05:00
```
JavaScript modules help you namespace, preventing accidental global variables.
## 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
NgModules are classes decorated with `@NgModule`. The `@NgModule` decorators `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` isnt being in their own file, like JavaScript modules; its the presence of `@NgModule` and its metadata.
The `AppModule` generated from the Angular CLI demonstrates both kinds of modules in action:
```typescript
2018-03-06 22:25:56 -05:00
/* These are JavaScript import statements. Angular doesnt 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({
declarations: [
AppComponent
],
imports: [ /* These are NgModule imports. */
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
2018-03-06 22:25:56 -05:00
```
The NgModule classes differ from JavaScript module in the following key ways:
* 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-07 01:35:57 -05:00
Angular模块只绑定了[_可声明的类_](guide/ngmodule-faq#q-declarable),这些可声明的类只是供[Angular编译器](guide/ngmodule-faq#q-angular-compiler)用的。
* 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
* 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-07 02:42:49 -05:00
Angular模块只能导出[_可声明的类_](guide/ngmodule-faq#q-declarable)。这可能是它自己拥有的也可能是从其它模块中导入的。它不会声明或导出任何其它类型的类。
* Unlike JavaScript modules, an NgModule can extend the _entire_ application with services
by adding providers to the `@NgModule.providers` list.
<hr />
## More on NgModules
For more information on NgModules, see:
2018-03-03 08:06:01 -05:00
* [Bootstrapping](guide/bootstrapping).
2018-03-03 08:06:01 -05:00
* [Frequently used modules](guide/frequent-ngmodules).
2018-03-03 08:06:01 -05:00
* [Providers](guide/providers).