angular-cn/aio/content/guide/deployment.md

527 lines
22 KiB
Markdown
Raw Normal View History

# Deployment
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/architecture.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/cb-index.md # aio/content/guide/change-log.md # aio/content/guide/cheatsheet.md # aio/content/guide/cli-quickstart.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/dynamic-component-loader.md # aio/content/guide/dynamic-form.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/hierarchical-dependency-injection.md # aio/content/guide/i18n.md # aio/content/guide/index.md # aio/content/guide/learning-angular.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/router.md # aio/content/guide/security.md # aio/content/guide/server-communication.md # aio/content/guide/set-document-title.md # aio/content/guide/setup-systemjs-anatomy.md # aio/content/guide/setup.md # aio/content/guide/structural-directives.md # aio/content/guide/styleguide.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/guide/ts-to-js.md # aio/content/guide/typescript-configuration.md # aio/content/guide/upgrade.md # aio/content/guide/user-input.md # aio/content/guide/visual-studio-2015.md # aio/content/guide/webpack.md # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/package.json # aio/src/styles/main.scss # aio/transforms/angular.io-package/index.js
2017-07-29 12:03:22 -04:00
# 部署
2017-03-31 19:57:13 -04:00
This page describes techniques for deploying your Angular application to a remote server.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
本章会描述在远程服务器上部署Angular应用的工具与技术。
{@a dev-deploy}
{@a copy-files}
2017-03-31 19:57:13 -04:00
## Simplest deployment possible
## 最简化的部署方式
For the simplest deployment, build for development and copy the output directory to a web server.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
最简化的部署方式就是为开发环境构建,并把其输出复制到 Web 服务器上。
1. Start with the development build
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
使用开发环境进行构建
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ng build
2018-03-03 08:06:01 -05:00
</code-example>
2. Copy _everything_ within the output folder (`dist/` by default) to a folder on the server.
2018-03-03 08:06:01 -05:00
把输出目录(默认为`dist/`)下的*每个文件*都复制到到服务器上的某个目录下。
3. If you copy the files into a server _sub-folder_, append the build flag, `--base-href` and set the `<base href>` appropriately.<br><br>
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
如果要把文件部署到服务器上的*某个子路径*下,构建时还要添加`--base-href`(基地址)标识,并设置合适的`<base href>`。<br><br>
For example, if the `index.html` is on the server at `/my/app/index.html`, set the _base href_ to
`<base href="/my/app/">` like this.
2018-03-03 08:06:01 -05:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
比如,如果 `index.html` 位于服务器上的 `/my/app/index.html` 路径下,就要把 *base href* 设置为 `<base href="/my/app/">`,就像这样:
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ng build --base-href=/my/app/
2018-03-03 08:06:01 -05:00
</code-example>
You'll see that the `<base href>` is set properly in the generated `dist/index.html`.<br><br>
If you copy to the server's root directory, omit this step and leave the `<base href>` alone.<br><br>
Learn more about the role of `<base href>` [below](guide/deployment#base-tag).
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
我们会看到在生成的 `dist/index.html``<base href>` 已经被设置好了。<br><br>
如果复制到服务器的根目录下,就省略这个步骤,并且让 `<base href>` 保持原样。<br><br>
要了解 `<base href>` 的作用,参见 [下面](guide/deployment#base-tag) 的内容。
2017-03-31 19:57:13 -04:00
4. Configure the server to redirect requests for missing files to `index.html`.
Learn more about server-side redirects [below](guide/deployment#fallback).
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
配置服务器,使其在找不到文件时把请求重定向到 `index.html`。要了解服务端重定向的更多知识,参见 [下面](guide/deployment#fallback) 的内容。
2018-03-03 08:06:01 -05:00
This is _not_ a production deployment. It's not optimized and it won't be fast for users.
It might be good enough for sharing your progress and ideas internally with managers, teammates, and other stakeholders.
这不是生产级部署。它没有优化过,并且对用户来说也不够快。
但是当你向经理、团队成员或其它利益相关者内部分享你的进度和想法时它是足够的。
{@a optimize}
## Optimize for production
Although deploying directly from the development environment works,
you can generate an optimized build with additional CLI command line flags,
starting with `--prod`.
### Build with _--prod_
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ng build --prod
2018-03-03 08:06:01 -05:00
</code-example>
The `--prod` _meta-flag_ engages the following optimization features.
* [Ahead-of-Time (AOT) Compilation](guide/aot-compiler): pre-compiles Angular component templates.
2018-03-03 08:06:01 -05:00
[预(AOT)编译](guide/aot-compiler):预编译 Angular 组件的模板。
* [Production mode](#enable-prod-mode): deploys the production environment which enables _production mode_.
2018-03-03 08:06:01 -05:00
[生产模式](#enable-prod-mode):启用生产模式部署到生产环境。
* Bundling: concatenates your many application and library files into a few bundles.
2018-03-03 08:06:01 -05:00
打捆Bundle把这些模块串接成一个单独的捆文件bundle
2017-03-31 19:57:13 -04:00
* Minification: removes excess whitespace, comments, and optional tokens.
2018-03-03 08:06:01 -05:00
最小化移除不必要的空格、注释和可选令牌Token
2017-03-31 19:57:13 -04:00
* Uglification: rewrites code to use short, cryptic variable and function names.
2018-03-03 08:06:01 -05:00
混淆:使用短的、无意义的变量名和函数名来重写代码。
* Dead code elimination: removes unreferenced modules and much unused code.
2018-03-03 08:06:01 -05:00
消除死代码:移除未引用过的模块和未使用过的代码。
The remaining [copy deployment steps](#copy-files) are the same as before.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
剩下的 [拷贝部署步骤](#copy-files) 和以前的方式是一样的。
2017-03-31 19:57:13 -04:00
You may further reduce bundle sizes by adding the `build-optimizer` flag.
2017-03-31 19:57:13 -04:00
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ng build --prod --build-optimizer
2018-03-03 08:06:01 -05:00
</code-example>
See the [CLI Documentation](https://github.com/angular/angular-cli/wiki/build)
for details about available build options and what they do.
{@a enable-prod-mode}
### Enable production mode
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
### 启用生产模式
Angular apps run in development mode by default, as you can see by the following message on the browser
console:
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
Angular应用默认运行在开发模式下正如在浏览器控制台中看到的如下信息
2017-03-31 19:57:13 -04:00
<code-example format="nocode">
2018-03-03 08:06:01 -05:00
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
2018-03-03 08:06:01 -05:00
</code-example>
Switching to _production mode_ can make it run faster by disabling development specific checks such as the dual change detection cycles.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
切换到生产模式可以通过禁用开发环境下特有的检查(比如双重变更检测周期)来让应用运行得更快。
Building for production (or appending the `--environment=prod` flag) enables _production mode_
Look at the CLI-generated `main.ts` to see how this works.
{@a lazy-loading}
### Lazy loading
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
### 惰性加载
You can dramatically reduce launch time by only loading the application modules that
absolutely must be present when the app starts.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
通过只加载应用启动时必须展示的那些应用模块,我们可以显著缩减启动时间。
2017-03-31 19:57:13 -04:00
Configure the Angular Router to defer loading of all other modules (and their associated code), either by
[waiting until the app has launched](guide/router#preloading "Preloading")
or by [_lazy loading_](guide/router#asynchronous-routing "Lazy loading")
them on demand.
2017-03-31 19:57:13 -04:00
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
配置Angular路由器可以延迟加载所有其它模块以及与它们相关的代码无论是[等应用启动](guide/router#preloading "Preloading")
还是在需要时才[惰性加载](guide/router#asynchronous-routing "Lazy loading")。
#### Don't eagerly import something from a lazy loaded module
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
#### 不要立即导入惰性加载模块中的任何东西
It's a common mistake.
You've arranged to lazy load a module.
But you unintentionally import it, with a JavaScript `import` statement,
in a file that's eagerly loaded when the app starts, a file such as the root `AppModule`.
If you do that, the module will be loaded immediately.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
这是一种常犯的错误。
我们本打算惰性加载一个模块,但可能无意中在根模块`AppModule`文件中使用一个JavaScript的`import`语句导入了它。
这样一来,该模块就被立即加载了。
The bundling configuration must take lazy loading into consideration.
Because lazy loaded modules aren't imported in JavaScript (as just noted), bundlers exclude them by default.
Bundlers don't know about the router configuration and won't create separate bundles for lazy loaded modules.
You have to create these bundles manually.
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/guide/ajs-quick-reference.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/component-interaction.md # aio/content/guide/component-styles.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/displaying-data.md # aio/content/guide/form-validation.md # aio/content/guide/forms.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule.md # aio/content/guide/npm-packages.md # aio/content/guide/pipes.md # aio/content/guide/quickstart.md # aio/content/guide/reactive-forms.md # aio/content/guide/structural-directives.md # aio/content/guide/template-syntax.md # aio/content/guide/testing.md # aio/content/marketing/index.html # aio/content/navigation.json # aio/content/tutorial/index.md # aio/content/tutorial/toh-pt1.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt3.md # aio/content/tutorial/toh-pt4.md # aio/content/tutorial/toh-pt5.md # aio/content/tutorial/toh-pt6.md # aio/src/app/embedded/api/api-list.component.ts # aio/src/app/layout/doc-viewer/doc-viewer.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/layout/toc/toc.component.html # aio/src/app/search/search.service.ts # aio/src/styles/1-layouts/_marketing-layout.scss # aio/yarn.lock
2018-02-27 19:08:59 -05:00
关于打包bundle方式的配置必须考虑到惰性加载问题。
因为惰性加载模块不能在JavaScript中导入就像刚才说明的打包器应该默认排除它们。
打包器不知道路由器的配置,并且不会为延迟加载模块创建单独的包。
我们不得不手动创建这些包。
The CLI runs the
[Angular Ahead-of-Time Webpack Plugin](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack)
which automatically recognizes lazy loaded `NgModules` and creates separate bundles for them.
{@a measure}
2017-03-31 19:57:13 -04:00
### Measure performance
You can make better decisions about what to optimize and how when you have a clear and accurate understanding of
what's making the application slow.
The cause may not be what you think it is.
You can waste a lot of time and money optimizing something that has no tangible benefit or even makes the app slower.
You should measure the app's actual behavior when running in the environments that are important to you.
The
<a href="https://developers.google.com/web/tools/chrome-devtools/network-performance/understanding-resource-timing" title="Chrome DevTools Network Performance">
Chrome DevTools Network Performance page</a> is a good place to start learning about measuring performance.
The [WebPageTest](https://www.webpagetest.org/) tool is another good choice
that can also help verify that your deployment was successful.
{@a inspect-bundle}
### Inspect the bundles
The <a href="https://github.com/danvk/source-map-explorer/blob/master/README.md">source-map-explorer</a>
tool is a great way to inspect the generated JavaScript bundles after a production build.
Install `source-map-explorer`:
2017-03-31 19:57:13 -04:00
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
npm install source-map-explorer --save-dev
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
Build your app for production _including the source maps_
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ng build --prod --sourcemaps
2018-03-03 08:06:01 -05:00
</code-example>
List the generated bundles in the `dist/` folder.
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
ls dist/*.bundle.js
2018-03-03 08:06:01 -05:00
</code-example>
Run the explorer to generate a graphical representation of one of the bundles.
The following example displays the graph for the _main_ bundle.
<code-example language="none" class="code-shell">
2018-03-03 08:06:01 -05:00
node_modules/.bin/source-map-explorer dist/main.*.bundle.js
2018-03-03 08:06:01 -05:00
</code-example>
The `source-map-explorer` analyzes the source map generated with the bundle and draws a map of all dependencies,
showing exactly which classes are included in the bundle.
Here's the output for the _main_ bundle of the QuickStart.
2017-03-31 19:57:13 -04:00
<figure>
2018-03-03 08:06:01 -05:00
<img src="generated/images/guide/cli-quickstart/quickstart-sourcemap-explorer.png" alt="quickstart sourcemap explorer">
2018-03-03 08:06:01 -05:00
</figure>
2017-03-31 19:57:13 -04:00
{@a base-tag}
## The `base` tag
The HTML [_&lt;base href="..."/&gt;_](/guide/router)
specifies a base path for resolving relative URLs to assets such as images, scripts, and style sheets.
For example, given the `<base href="/my/app/">`, the browser resolves a URL such as `some/place/foo.jpg`
into a server request for `my/app/some/place/foo.jpg`.
During navigation, the Angular router uses the _base href_ as the base path to component, template, and module files.
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
See also the [*APP_BASE_HREF*](api/common/APP_BASE_HREF "API: APP_BASE_HREF") alternative.
2017-04-10 11:51:13 -04:00
</div>
In development, you typically start the server in the folder that holds `index.html`.
That's the root folder and you'd add `<base href="/">` near the top of `index.html` because `/` is the root of the app.
But on the shared or production server, you might serve the app from a subfolder.
For example, when the URL to load the app is something like `http://www.mysite.com/my/app/`,
the subfolder is `my/app/` and you should add `<base href="/my/app/">` to the server version of the `index.html`.
When the `base` tag is mis-configured, the app fails to load and the browser console displays `404 - Not Found` errors
for the missing files. Look at where it _tried_ to find those files and adjust the base tag appropriately.
## _build_ vs. _serve_
2017-03-31 19:57:13 -04:00
You'll probably prefer `ng build` for deployments.
2017-03-31 19:57:13 -04:00
The **ng build** command is intended for building the app and deploying the build artifacts elsewhere.
The **ng serve** command is intended for fast, local, iterative development.
Both `ng build` and `ng serve` **clear the output folder** before they build the project.
The `ng build` command writes generated build artifacts to the output folder.
The `ng serve` command does not.
It serves build artifacts from memory instead for a faster development experience.
<div class="l-sub-section">
The output folder is `dist/` by default.
To output to a different folder, change the `outDir` in `.angular-cli.json`.
</div>
The `ng serve` command builds, watches, and serves the application from a local CLI development server.
The `ng build` command generates output files just once and does not serve them.
The `ng build --watch` command will regenerate output files when source files change.
This `--watch` flag is useful if you're building during development and
are automatically re-deploying changes to another server.
See the [CLI `build` topic](https://github.com/angular/angular-cli/wiki/build) for more details and options.
<hr>
{@a server-configuration}
## Server configuration
## 服务端配置
This section covers changes you may have make to the server or to files deployed to the server.
这一节涵盖了我们对服务器或准备部署到服务器的文件要做的那些修改。
{@a fallback}
2017-03-31 19:57:13 -04:00
### Routed apps must fallback to `index.html`
2017-09-06 19:22:51 -04:00
### 带路由的应用必须以`index.html`作为后备页面
Angular apps are perfect candidates for serving with a simple static HTML server.
You don't need a server-side engine to dynamically compose application pages because
Angular does that on the client-side.
Angular应用很适合用简单的静态HTML服务器提供服务。
我们不需要服务端引擎来动态合成应用页面因为Angular会在客户端完成这件事。
If the app uses the Angular router, you must configure the server
to return the application's host page (`index.html`) when asked for a file that it does not have.
如果该应用使用Angular路由器我们就必须配置服务器让它对不存在的文件返回应用的宿主页(`index.html`)。
{@a deep-link}
2017-03-31 19:57:13 -04:00
A routed application should support "deep links".
A _deep link_ is a URL that specifies a path to a component inside the app.
For example, `http://www.mysite.com/heroes/42` is a _deep link_ to the hero detail page
that displays the hero with `id: 42`.
带路由的应用应该支持“深链接”。
所谓*深链接*就是指一个URL它用于指定到应用内某个组件的路径。
比如,`http://www.mysite.com/heroes/42`就是一个到英雄详情页面的*深链接*,用于显示`id: 42`的英雄。
There is no issue when the user navigates to that URL from within a running client.
The Angular router interprets the URL and routes to that page and hero.
当用户从运行中的客户端应用导航到这个URL时这没问题。
Angular路由器会拦截这个URL并且把它路由到正确的页面。
But clicking a link in an email, entering it in the browser address bar,
or merely refreshing the browser while on the hero detail page &mdash;
all of these actions are handled by the browser itself, _outside_ the running application.
The browser makes a direct request to the server for that URL, bypassing the router.
但是,当从邮件中点击链接或在浏览器地址栏中输入它或仅仅在英雄详情页刷新下浏览器时,所有这些操作都是由浏览器本身处理的,在应用的控制范围之外。
浏览器会直接向服务器请求那个URL路由器没机会插手。
A static server routinely returns `index.html` when it receives a request for `http://www.mysite.com/`.
But it rejects `http://www.mysite.com/heroes/42` and returns a `404 - Not Found` error *unless* it is
configured to return `index.html` instead.
静态服务器会在收到对`http://www.mysite.com/`的请求时返回`index.html`,但是会拒绝对`http://www.mysite.com/heroes/42`的请求,
并返回一个`404 - Not Found`错误,除非,我们把它配置成转而返回`index.html`。
#### Fallback configuration examples
#### 后备页面配置范例
There is no single configuration that works for every server.
The following sections describe configurations for some of the most popular servers.
The list is by no means exhaustive, but should provide you with a good starting point.
没有一种配置可以适用于所有服务器。
后面这些部分会描述对常见服务器的配置方式。
这个列表虽然不够详尽,但可以为你提供一个良好的起点。
#### Development servers
#### 开发服务器
2017-03-31 19:57:13 -04:00
* [Lite-Server](https://github.com/johnpapa/lite-server): the default dev server installed with the
[Quickstart repo](https://github.com/angular/quickstart) is pre-configured to fallback to `index.html`.
2018-03-03 08:06:01 -05:00
[Lite-Server](https://github.com/johnpapa/lite-server)是["快速上手"仓库](https://github.com/angular/quickstart)中安装的默认开发服务器,它被预先配置为回退到`index.html`。
2017-03-31 19:57:13 -04:00
* [Webpack-Dev-Server](https://github.com/webpack/webpack-dev-server): setup the
`historyApiFallback` entry in the dev server options as follows:
2018-03-03 08:06:01 -05:00
[Webpack-Dev-Server](https://github.com/webpack/webpack-dev-server)在开发服务器的配置中设置了`historyApiFallback`,代码如下:
<code-example>
2018-03-03 08:06:01 -05:00
historyApiFallback: {
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
}
2017-03-31 19:57:13 -04:00
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
#### Production servers
#### 生产服务器
2017-03-31 19:57:13 -04:00
* [Apache](https://httpd.apache.org/): add a
2018-03-03 08:06:01 -05:00
[rewrite rule](http://httpd.apache.org/docs/current/mod/mod_rewrite.html) to the `.htaccess` file as shown
(https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/):
2018-02-28 00:43:40 -05:00
2018-03-03 08:06:01 -05:00
[Apache](https://httpd.apache.org/):在`.htaccess`文件中添加一个[重写规则](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)
代码如下([出处](https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/)
<code-example format=".">
2018-03-03 08:06:01 -05:00
RewriteEngine On
&#35 If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
2017-03-31 19:57:13 -04:00
&#35 If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
2017-03-31 19:57:13 -04:00
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
* [NGinx](http://nginx.org/): use `try_files`, as described in
[Front Controller Pattern Web Apps](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#front-controller-pattern-web-apps),
modified to serve `index.html`:
2018-03-03 08:06:01 -05:00
[NGinx](http://nginx.org/):使用`try_files`指向`index.html`,详细描述见[Web应用的前端控制器模式](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#front-controller-pattern-web-apps)。
2018-03-03 08:06:01 -05:00
<code-example format=".">
2017-03-31 19:57:13 -04:00
2018-03-03 08:06:01 -05:00
try_files $uri $uri/ /index.html;
2018-03-03 08:06:01 -05:00
</code-example>
2017-03-31 19:57:13 -04:00
* [IIS](https://www.iis.net/): add a rewrite rule to `web.config`, similar to the one shown
[here](http://stackoverflow.com/a/26152011/2116927):
2018-03-03 08:06:01 -05:00
[IIS](https://www.iis.net/):往`web.config`中添加一条重写规则,类似于[这里](http://stackoverflow.com/a/26152011/2116927)
<code-example format='.'>
&lt;system.webServer&gt;
&lt;rewrite&gt;
&lt;rules&gt;
&lt;rule name="Angular Routes" stopProcessing="true"&gt;
&lt;match url=".*" /&gt;
&lt;conditions logicalGrouping="MatchAll"&gt;
&lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt;
&lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt;
&lt;/conditions&gt;
&lt;action type="Rewrite" url="/index.html" /&gt;
&lt;/rule&gt;
&lt;/rules&gt;
&lt;/rewrite&gt;
&lt;/system.webServer&gt;
2017-03-31 19:57:13 -04:00
</code-example>
2017-03-31 19:57:13 -04:00
* [GitHub Pages](https://pages.github.com/): you can't
[directly configure](https://github.com/isaacs/github/issues/408)
the GitHub Pages server, but you can add a 404 page.
Copy `index.html` into `404.html`.
It will still be served as the 404 response, but the browser will process that page and load the app properly.
It's also a good idea to
[serve from `docs/` on master](https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/#publishing-your-github-pages-site-from-a-docs-folder-on-your-master-branch)
and to
[create a `.nojekyll` file](https://www.bennadel.com/blog/3181-including-node-modules-and-vendors-folders-in-your-github-pages-site.htm)
2018-03-03 08:06:01 -05:00
[GitHub页面服务](https://pages.github.com/):我们没办法[直接配置](https://github.com/isaacs/github/issues/408) Github的页面服务但可以添加一个404页只要把`index.html`复制到`404.html`就可以了。
它仍然会给出一个404响应但是浏览器将会正确处理该页并正常加载该应用。
使用[在主分支的`docs/`下启动服务](https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/#publishing-your-github-pages-site-from-a-docs-folder-on-your-master-branch)
并[创建一个`.nojekyll`文件](https://www.bennadel.com/blog/3181-including-node-modules-and-vendors-folders-in-your-github-pages-site.htm)也是一个好办法。
2017-03-31 19:57:13 -04:00
* [Firebase hosting](https://firebase.google.com/docs/hosting/): add a
[rewrite rule](https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites).
2018-03-03 08:06:01 -05:00
[Firebase主机服务](https://firebase.google.com/docs/hosting/):添加一条[重写规则](https://firebase.google.com/docs/hosting/url-redirects-rewrites#section-rewrites)。
2018-03-03 08:06:01 -05:00
<code-example format=".">
2017-03-31 19:57:13 -04:00
2018-03-03 08:06:01 -05:00
"rewrites": [ {
"source": "**",
"destination": "/index.html"
} ]
</code-example>
{@a cors}
### Requesting services from a different server (CORS)
### 请求来自另一个服务器的服务CORS
Angular developers may encounter a
<a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing" title="Cross-origin resource sharing">
<i>cross-origin resource sharing</i></a> error when making a service request (typically a data service request)
to a server other than the application's own host server.
Browsers forbid such requests unless the server permits them explicitly.
Angular开发者在向与该应用的宿主服务器不同域的服务器发起请求时可能会遇到一种<a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing" target="_blank" title="Cross-origin resource sharing"><i>跨域资源共享CORS</i></a>错误。
浏览器会阻止该请求,除非得到那台服务器的明确许可。
There isn't anything the client application can do about these errors.
The server must be configured to accept the application's requests.
Read about how to enable CORS for specific servers at
<a href="http://enable-cors.org/server.html" title="Enabling CORS server">enable-cors.org</a>.
2018-03-03 08:06:01 -05:00
客户端应用对这种错误无能为力。
服务器必须配置成可以接受来自该应用的请求。
2018-03-03 08:06:01 -05:00
要了解如何对特定的服务器开启CORS参见<a href="http://enable-cors.org/server.html" target="_blank" title="Enabling CORS server">enable-cors.org</a>