问题
Angular 2 modules allows app to divided into multiple modules each serving a different purpose. But isn't that what ES6 modules also supposed to do?
What is the difference between the two?
回答1:
Taken from angular.io:
The Angular module classes differ from JavaScript module class in three key respects:
An Angular module bounds declarable classes only. Declarables are the only classes that matter to the Angular.
Instead of defining all member classes in one giant file (as in a JavaScript module), we list the module's classes in the
@NgModule.declarations
list.- An Angular module can only export the declarable classes it owns or imports from other modules. It doesn't declare or export any other kind of class.
回答2:
I found great article explaining difference here, Here are key differences:
ES6 modules:
- The ES6 Modules ,which also goes by the name JS modules or JavaScript modules or ECMAScript modules are part of the JavaScript Language.
- The JS Modules are stored in a file. There is exactly one module per file and one file per module.
These modules contains a small units of independent, reusable code. They export a value, which can be imported and used in some other module.
Purpose of ES6 Modules:
Avoid leaking code to the global namespace,
- Encapsulate code to hide implementation details,
Angular Modules:
Angular Modules are an Angular-specific construct.
Angular Modules logically group different Angular artifacts such as components, pipes, directives, etc.
Angular Modules in the form of the @NgModule decorator provide metadata to the Angular compiler which in turn can better “reason about our application” structure and thus introduce optimizations.
Purpose of Angular Modules:
organize the Angular application parts into cohesive blocks
helps to keep the Separation of concerns
回答3:
ES modules vs Angular modules:
ES modules are code files that import or export something while angular modules organize the application into cohesive blocks of functionality.
ES modules organize our code while angular modules organize our application.
ES modules modularize our code while angular modules modularize our application.
ES modules promote code reuse while angular modules promote application boundaries.
回答4:
Taken from this source: Angular Modules vs ES6 Modules
ES Modules:
- ES6 modules represent a single file.
- The ES6 module syntax is a standardized construct of the ECMAScript language specification.
On the other side:
- Angular Modules are an Angular-specific construct.
- Angular Modules logically group different Angular artifacts such as components, pipes, directives, etc.
- Angular Modules in the form of the @NgModule decorator provide metadata to the Angular compiler which in turn can better “reason about our application” structure and thus introduce optimizations.
- Important features such as lazy loading are done at the Angular Module level.
来源:https://stackoverflow.com/questions/42297888/difference-between-angular-2-modules-and-javascript-es6-modules