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

168 lines
9.7 KiB
Markdown
Raw Normal View History

2018-03-24 04:33:17 -04:00
# Architecture overview
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
2018-03-24 01:12:42 -04:00
Angular is a platform and framework for building client applications in HTML and TypeScript.
Angular is itself written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that you import into your apps.
2018-03-24 01:12:42 -04:00
Angular 是一个用 HTML 和 JavaScript 或者一个可以编译成 JavaScript 的语言(例如 Dart 或者 TypeScript ),来构建客户端应用的平台和框架。
2018-03-24 01:12:42 -04:00
The basic building blocks of an Angular application are _NgModules_, which provide a compilation context for _components_. NgModules collect related code into functional sets; an Angular app is defined by a set of NgModules. An app always has at least a _root module_ that enables bootstrapping, and typically has many more _feature modules_.
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
* Components define *views*, which are sets of screen elements that Angular can choose among and modify according to your program logic and data. Every app has at least a root component.
2018-03-24 01:12:42 -04:00
* Components use *services*, which provide specific functionality not directly related to views. Service providers can be *injected* into components as *dependencies*, making your code modular, reusable, and efficient.
2018-03-24 01:12:42 -04:00
Both components and services are simply classes, with *decorators* that mark their type and provide metadata that tells Angular how to use them.
2018-03-24 01:12:42 -04:00
* The metadata for a component class associates it with a *template* that defines a view. A template combines ordinary HTML with Angular *directives* and *binding markup* that allow Angular to modify the HTML before rendering it for display.
2018-03-24 01:12:42 -04:00
* The metadata for a service class provides the information Angular needs to make it available to components through *Dependency Injection (DI)*.
2018-03-24 01:12:42 -04:00
An app's components typically define many views, arranged hierarchically. Angular provides the `Router` service to help you define navigation paths among views. The router provides sophisticated in-browser navigational capabilities.
## Modules
## 模块
2018-03-24 01:12:42 -04:00
Angular defines the `NgModule`, which differs from and complements the JavaScript (ES2015) module. An NgModule declares a compilation context for a set of components that is dedicated to an application domain, a workflow, or a closely related set of capabilities. An NgModule can associate its components with related code, such as services, to form functional units.
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
Every Angular app has a _root module_, conventionally named `AppModule`, which provides the bootstrap mechanism that launches the application. An app typically contains many functional modules.
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
Like JavaScript modules, NgModules can import functionality from other NgModules, and allow their own functionality to be exported and used by other NgModules. For example, to use the router service in your app, you import the `Router` NgModule.
2018-03-24 01:12:42 -04:00
Organizing your code into distinct functional modules helps in managing development of complex applications, and in designing for reusability. In addition, this technique lets you take advantage of _lazy-loading_—that is, loading modules on demand—in order to minimize the amount of code that needs to be loaded at startup.
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2018-03-24 01:12:42 -04:00
For a more detailed discussion, see [Introduction to modules](guide/architecture-modules).
2017-04-10 11:51:13 -04:00
</div>
2018-03-24 01:12:42 -04:00
## Components
2018-03-24 01:12:42 -04:00
## 组件
2018-03-24 01:12:42 -04:00
Every Angular application has at least one component, the *root component* that connects a component hierarchy with the page DOM. Each component defines a class that contains application data and logic, and is associated with an HTML *template* that defines a view to be displayed in a target environment.
2018-03-24 01:12:42 -04:00
The `@Component` decorator identifies the class immediately below it as a component, and provides the template and related component-specific metadata.
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2018-03-24 01:12:42 -04:00
Decorators are functions that modify JavaScript classes. Angular defines a number of such decorators that attach specific kinds of metadata to classes, so that it knows what those classes mean and how they should work.
2018-03-24 01:12:42 -04:00
<a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841#.x5c2ndtx0">Learn more about decorators on the web.</a>
2017-04-10 11:51:13 -04:00
</div>
2018-03-24 01:12:42 -04:00
### Templates, directives, and data binding
2018-03-24 01:12:42 -04:00
A template combines HTML with Angular markup that can modify the HTML elements before they are displayed.
Template *directives* provide program logic, and *binding markup* connects your application data and the document object model (DOM).
2018-03-03 08:06:01 -05:00
2018-03-24 01:12:42 -04:00
* *Event binding* lets your app respond to user input in the target environment by updating your application data.
2018-03-24 01:12:42 -04:00
* *Property binding* lets you interpolate values that are computed from your application data into the HTML.
2018-03-24 01:12:42 -04:00
Before a view is displayed, Angular evaluates the directives and resolves the binding syntax in the template to modify the HTML elements and the DOM, according to your program data and logic. Angular supports *two-way data binding*, meaning that changes in the DOM, such as user choices, can also be reflected back into your program data.
2018-03-24 01:12:42 -04:00
Your templates can also use *pipes* to improve the user experience by transforming values for display. Use pipes to display, for example, dates and currency values in a way appropriate to the user's locale. Angular provides predefined pipes for common transformations, and you can also define your own.
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2018-03-24 01:12:42 -04:00
For a more detailed discussion of these concepts, see [Introduction to components](guide/architecture-components).
2017-03-31 19:57:13 -04:00
2017-04-10 11:51:13 -04:00
</div>
2018-03-24 01:12:42 -04:00
{@a dependency-injection}
2018-03-24 01:12:42 -04:00
## Services and dependency injection
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
For data or logic that is not associated with a specific view, and that you want to share across components, you create a *service* class. A service class definition is immediately preceded by the `@Injectable` decorator. The decorator provides the metadata that allows your service to be *injected* into client components as a dependency.
2018-03-24 01:12:42 -04:00
*Dependency injection* (or DI) lets you keep your component classes lean and efficient. They don't fetch data from the server, validate user input, or log directly to the console; they delegate such tasks to services.
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2018-03-24 01:12:42 -04:00
For a more detailed discusssion, see [Introduction to services and DI](guide/architecture-services).
2017-04-10 11:51:13 -04:00
</div>
2018-03-24 01:12:42 -04:00
### Routing
2018-03-24 01:12:42 -04:00
The Angular `Router` NgModule provides a service that lets you define a navigation path among the different application states and view hierarchies in your app. It is modeled on the familiar browser navigation conventions:
2018-03-24 01:12:42 -04:00
* Enter a URL in the address bar and the browser navigates to a corresponding page.
2018-03-24 01:12:42 -04:00
在地址栏输入 URL浏览器就会导航到相应的页面。
2018-03-24 01:12:42 -04:00
* Click links on the page and the browser navigates to a new page.
2018-03-24 01:12:42 -04:00
在页面中点击链接,浏览器就会导航到一个新页面。
2018-03-24 01:12:42 -04:00
* Click the browser's back and forward buttons and the browser navigates backward and forward through the history of pages you've seen.
2018-03-24 01:12:42 -04:00
点击浏览器的前进和后退按钮,浏览器就会在你的浏览历史中向前或向后导航。
2018-03-24 01:12:42 -04:00
The router maps URL-like paths to views instead of pages. When a user performs an action, such as clicking a link, that would load a new page in the browser, the router intercepts the browser's behavior, and shows or hides view hierarchies.
2018-03-24 01:12:42 -04:00
If the router determines that the current application state requires particular functionality, and the module that defines it has not been loaded, the router can _lazy-load_ the module on demand.
2018-03-24 01:12:42 -04:00
The router interprets a link URL according to your app's view navigation rules and data state. You can navigate to new views when the user clicks a button, selects from a drop box, or in response to some other stimulus from any source. The Router logs activity in the browser's history journal, so the back and forward buttons work as well.
2018-03-24 01:12:42 -04:00
To define navigation rules, you associate *navigation paths* with your components. A path uses a URL-like syntax that integrates your program data, in much the same way that template syntax integrates your views with your program data. You can then apply program logic to choose which views to show or to hide, in response to user input and your own access rules.
2018-03-24 01:12:42 -04:00
<div class="l-sub-section">
docs(aio): image sweep (#16609) * fix(aio): allow code blocks to clear floated images Previously the negative margin on the code headings were causing floated images to overlay the start of a code block. Now all code block successfully clear all floated elements. * feat(aio): add a `.clear` class for clearing floating images * fix(aio): tidy up image styles The css rules for `img.right` and `img.left` allow authors easy access to floating an image on the left or right, respectively. The `.image-display` rule which was always found on a figure has been simplified so that all figures have this styling. It is very unlikely that a figure will be used outside the content area; and at this time it seems like `figure` is as good an indicator that we want this kind of styling as anything. Now that images are all tagged with width and height values, we cannot assume to modify these dimensions via CSS as it can cause the image to lose its correct proportions. Until we find a better solition we must set `height` to `auto` when the screen width is below 1300px to ensure that these images maintain their proportions as they get shrunk to fit. * docs(aio): general tidy up of image HTML in guides Previously, the guides have a lot of inline image styling and unnecessary use of the `image-display` css class. Images over 700px are problematic for guide docs, so those have been given specific widths and associated heights. * docs(aio): use correct anchor for "back to the top" link The `#toc` anchor does not work when the page is wide enough that the TOC is floating to the side. * build(aio): add `#top-of-page` to path variants for link checking Since the `#top-of-page` is outside the rendered docs the `checkAnchorLinks` processor doesn't find them as valid targets for links. Adding them as a `pathVariant` solves this problem but will still catch links to docs that do not actually exist. * fix(aio): ensure that headings clear floated images * fix(aio): do not force live-example embedded image to 100% size This made them look too big, generally. Leaving them with no size means that they will look reasonable in large viewports and switch to 100% width in narrow viewports.
2017-05-09 18:53:32 -04:00
2018-03-24 01:12:42 -04:00
For a more detailed discussion, see [Routing and navigation](guide/router).
2018-03-24 01:12:42 -04:00
</div>
2017-04-10 11:51:13 -04:00
<hr/>
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
## What's next
2017-03-31 19:57:13 -04:00
2018-03-24 04:33:17 -04:00
## 接下来呢?
2018-03-24 01:12:42 -04:00
You've learned the basics about the main building blocks of an Angular application. The following diagram shows how these basic pieces are related.
<figure>
2018-03-24 01:12:42 -04:00
<img src="generated/images/guide/architecture/overview2.png" alt="overview">
</figure>
2018-03-24 01:12:42 -04:00
* Together, a component and template define an Angular view.
2018-03-24 01:12:42 -04:00
* A decorator on a component class adds the metadata, including a pointer to the associated template.
2018-03-24 01:12:42 -04:00
* Directives and binding markup in a component's template modify views based on program data and logic.
2018-03-24 01:12:42 -04:00
* The dependency injector provides services to a component, such as the router service that lets you define navigation among views.
2018-03-24 01:12:42 -04:00
Each of these subjects is introduced in more detail in the following pages.
2018-03-24 01:12:42 -04:00
* [Modules](guide/architecture-modules)
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
* [Components](guide/architecture-components)
2018-03-24 01:12:42 -04:00
* [Templates](guide/architecture-components#templates-and-views)
2018-03-24 01:12:42 -04:00
* [Metadata](guide/architecture-components#component-metadata)
2017-03-31 19:57:13 -04:00
2018-03-24 01:12:42 -04:00
* [Data binding](guide/architecture-components#data-binding)
2018-03-24 01:12:42 -04:00
* [Directives](guide/architecture-components#directives)
2018-03-24 01:12:42 -04:00
* [Pipes](guide/architecture-components#pipes)
2018-03-24 01:12:42 -04:00
* [Services and dependency injection](guide/architecture-services)
2017-04-10 11:51:13 -04:00
<div class="l-sub-section">
2018-03-24 01:12:42 -04:00
Note that the code referenced on these pages is available as a <live-example></live-example>.
2017-04-10 11:51:13 -04:00
</div>
2018-03-24 01:12:42 -04:00
When you are familiar with these fundamental building blocks, you can explore them in more detail in the documentation. To learn about more tools and techniques that are available to help you build and deploy Angular applications, see [Next steps](guide/architecture-next-steps).
2018-03-24 01:12:42 -04:00
</div>