Angular applications and Angular itself depend upon features and functionality provided by a variety of third-party packages.
These packages are maintained and installed with the Node Package Manager (npm).
Node.js和npm是做Angular开发的基础。
Node.js and npm are essential to Angular development.
Verify that you are running node v4.x.x or higher and npm 3.x.x or higher
by running the commands node -v and npm -v in a terminal/console window.
Older versions produce errors.
Consider using nvm for managing multiple
versions of node and npm. You may need nvm if
you already have projects running on your machine that use other versions of node and npm.
During Setup, a package.json
file is installed with a comprehensive starter set of
packages as specified in the dependencies and devDependencies sections.
You can use other packages but the packages in this particular set work well together and include
everything you need to build and run the sample applications in this series.
注意:烹饪宝典或开发指南中的页面可能需要其它库,比如jQuery。
Note: A cookbook or guide page may require an additional library such as jQuery.
You'll install more than you need for the QuickStart guide.
No worries!
You only serve to the client those packages that the application actually requests.
本页面会解释每一个包是干什么的,以后你就可以根据自己的喜好和经验,随意替换它们了。
This page explains what each package does. You can make substitutions later to suit your tastes and experience.
The dependencies are essential to running the application.
The devDependencies are only necessary to develop the application.
You can exclude them from production installations by adding --production to the install command, as follows:
npm install my-application --production
dependencies
应用程序的package.json文件中,dependencies区下有三类包:
The dependencies section of package.json contains:
特性* - 特性包为应用程序提供了框架和工具方面的能力。
Features : Feature packages give the application framework and utility capabilities.
填充(Polyfills) - 填充包弥合了不同浏览器上的JavaScript实现方面的差异。
Polyfills : Polyfills plug gaps in the browser's JavaScript implementation.
其它 - 其它库对本应用提供支持,比如bootstrap包提供了HTML中的小部件和样式。
Other : Other libraries that support the application such as bootstrap for HTML widgets and styling.
@angular/core : Critical runtime parts of the framework needed by every application.
Includes all metadata decorators, Component, Directive, dependency injection, and the component lifecycle hooks.
@angular/common - 常用的那些由Angular开发组提供的服务、管道和指令。
@angular/common : The commonly needed services, pipes, and directives provided by the Angular team.
@angular/compiler : Angular's Template Compiler.
It understands templates and can convert them to code that makes the application run and render.
Typically you don’t interact with the compiler directly; rather, you use it indirectly via platform-browser-dynamic or the offline template compiler.
@angular/platform-browser : Everything DOM and browser related, especially the pieces that help render into theDOM.
This package also includes the bootstrapStatic() method for bootstrapping applications for production builds that pre-compile templates offline.
@angular/platform-browser-dynamic : Includes Providers and a bootstrap method for applications that
compile templates on the client. Don’t use offline compilation.
Use this package for bootstrapping during development and for bootstrapping plunker samples.
@angular/http - Angular的HTTP客户端。
@angular/http : Angular's HTTP client.
@angular/router - 路由器。
@angular/router : Component router.
@angular/upgrade - 一组用于升级AngularJS应用的工具。
@angular/upgrade : Set of utilities for upgrading AngularJS applications to Angular.
Angular requires certain polyfills in the application environment.
Install these polyfills using the npm packages that Angular lists in the peerDependencies section of its package.json.
但我们必须把它列在我们package.json文件的dependencies区。
You must list these packages in the dependencies section of your own package.json.
core-js: Patches the global context (window) with essential features of ES2015 (ES6).
You may substitute an alternative polyfill that provides the same core APIs.
When these APIs are implemented by the major browsers, this dependency will become unnecessary.
rxjs : A polyfill for the Observables specification currently before the
TC39 committee that determines standards for the JavaScript language.
You can pick a preferred version of rxjs (within a compatible version range)
without waiting for Angular updates.
zone.js : A polyfill for the Zone specification currently before the
TC39 committee that determines standards for the JavaScript language.
You can pick a preferred version of zone.js to use (within a compatible version range)
without waiting for Angular updates.
angular-in-memory-web-api : An Angular-supported library that simulates a remote server's web api
without requiring an actual server or real HTTP calls.
Good for demos, samples, and early stage development (before you even have a server).
Read about it in the HTTP Client page.
bootstrap : Bootstrap is a popular HTML and CSS framework for designing responsive web apps.
Some of the samples improve their appearance with bootstrap.
The packages listed in the devDependencies section of the package.json help you develop the application.
You don't have to deploy them with the production application although there is no harm in doing so.
concurrently - 一个用来在OS/X、Windows和Linux操作系统上同时运行多个npm命令的工具
lite-server :
A light-weight, static file server, by John Papa
with excellent support for Angular apps that use routing.
concurrently :
A utility to run multiple npm commands concurrently on OS/X, Windows, and Linux operating systems.
There isn't a peerDependencies section in the QuickStart package.json.
But Angular has a peerDependencies section in
itspackage.json, which has important consequences for your application.
This section explains why you load the polyfilldependency packages in the QuickStart application'spackage.json,
and why you'll need those packages in your own applications.
What if "A" and "B" depend on different versions of "C" ("C1" and "C2"). The npm package system supports that.
It installs "C1" in the node_modules folder for "A" and "C2" in the node_modules folder for "B".
Now "A" and "B" have their own copies of "C" and they run without interferring with one another.
But there is a problem. Package "A" may require the presence of "C1" without actually calling upon it directly.
"A" may only work if everyone is using "C1". It falls down if any part of the application relies on "C2".
要想解决这个问题,“A”就需要把“C1”定义为它的平级依赖。
The solution is for "A" to declare that "C1" is a peer dependency.
在dependencies和peerDependencies之间的区别大致是这样的:
The difference between a dependency and a peerDependency is roughly this:
dependency说:“我需要这东西对我是直接可用的。”
A dependency says, "I need this thing directly available to me."
peerDependency说:“如果你想使用我,你得先确保这东西对你是可用的”
A peerDependency says, "If you want to use me, you need this thing available to you."
When npm installs packages listed in yourdependencies section,
it also installs the packages listed within their packages dependencies sections.
The process is recursive.
但是在npm的第三版中,它不会安装列在peerDependencies区的那些包。
However, as of version 3, npm does not install packages listed in peerDependencies sections.
Fortunately, npm issues a warning (a) When any peer dependencies are missing, or (b)
When the application or any of its other dependencies
installs a different version of a peer dependency.
这些警告可以避免因为版本不匹配而导致的意外错误。
它们让我们可以控制包和版本的解析过程。
These warnings guard against accidental failures due to version mismatches.
They leave you in control of package and version resolution.
我们的责任是,把所有平级依赖包都列在我们自己的devDependencies中。
It is your responsibility to list all peer dependency packages among your own devDependencies.
However, there is an npm feature request for "optional peerDependencies," which would allow you to model this relationship better.
When this feature request is implemented, Angular will switch from peerDependencies to optionalPeerDependencies for all polyfills.