angular-cn/aio/dist/generated/docs/guide/architecture-services.json

5 lines
11 KiB
JSON

{
"id": "guide/architecture-services",
"title": "Introduction to services and dependency injection",
"contents": "\n\n\n<div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/aio/content/guide/architecture-services.md?message=docs%3A%20describe%20your%20change...\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n</div>\n\n\n<div class=\"content\">\n <h1 id=\"introduction-to-services-and-dependency-injection\">Introduction to services and dependency injection<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/architecture-services#introduction-to-services-and-dependency-injection\"><i class=\"material-icons\">link</i></a></h1>\n<p><em>Service</em> is a broad category encompassing any value, function, or feature that an app needs.\nA service is typically a class with a narrow, well-defined purpose.\nIt should do something specific and do it well.</p>\n<p>Angular distinguishes components from services to increase modularity and reusability.\nBy separating a component's view-related functionality from other kinds of processing,\nyou can make your component classes lean and efficient.</p>\n<p>Ideally, a component's job is to enable the user experience and nothing more.\nA component should present properties and methods for data binding,\nin order to mediate between the view (rendered by the template)\nand the application logic (which often includes some notion of a <em>model</em>).</p>\n<p>A component can delegate certain tasks to services, such as fetching data from the server,\nvalidating user input, or logging directly to the console.\nBy defining such processing tasks in an <em>injectable service class</em>, you make those tasks\navailable to any component.\nYou can also make your app more adaptable by injecting different providers of the same kind of service,\nas appropriate in different circumstances.</p>\n<p>Angular doesn't <em>enforce</em> these principles. Angular does help you <em>follow</em> these principles\nby making it easy to factor your application logic into services and make those services\navailable to components through <em>dependency injection</em>.</p>\n<h2 id=\"service-examples\">Service examples<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/architecture-services#service-examples\"><i class=\"material-icons\">link</i></a></h2>\n<p>Here's an example of a service class that logs to the browser console.</p>\n<code-example path=\"architecture/src/app/logger.service.ts\" header=\"src/app/logger.service.ts (class)\" region=\"class\">\nexport class Logger {\n log(msg: any) { console.log(msg); }\n error(msg: any) { console.error(msg); }\n warn(msg: any) { console.warn(msg); }\n}\n\n\n</code-example>\n<p>Services can depend on other services. For example, here's a <code>HeroService</code> that depends on the <code>Logger</code> service, and also uses <code>BackendService</code> to get heroes. That service in turn might depend on the <code><a href=\"api/common/http/HttpClient\" class=\"code-anchor\">HttpClient</a></code> service to fetch heroes asynchronously from a server.</p>\n<code-example path=\"architecture/src/app/hero.service.ts\" header=\"src/app/hero.service.ts (class)\" region=\"class\">\nexport class HeroService {\n private heroes: Hero[] = [];\n\n constructor(\n private backend: BackendService,\n private logger: Logger) { }\n\n getHeroes() {\n this.backend.getAll(Hero).then( (heroes: Hero[]) => {\n this.logger.log(`Fetched ${heroes.length} heroes.`);\n this.heroes.push(...heroes); // fill cache\n });\n return this.heroes;\n }\n}\n\n\n</code-example>\n<h2 id=\"dependency-injection-di\">Dependency injection (DI)<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/architecture-services#dependency-injection-di\"><i class=\"material-icons\">link</i></a></h2>\n<img src=\"generated/images/guide/architecture/dependency-injection.png\" alt=\"Service\" class=\"left\" width=\"200\" height=\"90\">\n<p>DI is wired into the Angular framework and used everywhere to provide new components with the services or other things they need.\nComponents consume services; that is, you can <em>inject</em> a service into a component, giving the component access to that service class.</p>\n<p>To define a class as a service in Angular, use the <code>@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()</code> decorator to provide the metadata that allows Angular to inject it into a component as a <em>dependency</em>.\nSimilarly, use the <code>@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()</code> decorator to indicate that a component or other class (such as another service, a pipe, or an NgModule) <em>has</em> a dependency.</p>\n<ul>\n<li>\n<p>The <em>injector</em> is the main mechanism. Angular creates an application-wide injector for you during the bootstrap process, and additional injectors as needed. You don't have to create injectors.</p>\n</li>\n<li>\n<p>An injector creates dependencies, and maintains a <em>container</em> of dependency instances that it reuses if possible.</p>\n</li>\n<li>\n<p>A <em>provider</em> is an object that tells an injector how to obtain or create a dependency.</p>\n</li>\n</ul>\n<p>For any dependency that you need in your app, you must register a provider with the app's injector,\nso that the injector can use the provider to create new instances.\nFor a service, the provider is typically the service class itself.</p>\n<div class=\"alert is-helpful\">\n<p>A dependency doesn't have to be a service—it could be a function, for example, or a value.</p>\n</div>\n<p>When Angular creates a new instance of a component class, it determines which services or other dependencies that component needs by looking at the constructor parameter types. For example, the constructor of <code>HeroListComponent</code> needs <code>HeroService</code>.</p>\n<code-example path=\"architecture/src/app/hero-list.component.ts\" header=\"src/app/hero-list.component.ts (constructor)\" region=\"ctor\">\nconstructor(private service: HeroService) { }\n\n</code-example>\n<p>When Angular discovers that a component depends on a service, it first checks if the injector has any existing instances of that service. If a requested service instance doesn't yet exist, the injector makes one using the registered provider, and adds it to the injector before returning the service to Angular.</p>\n<p>When all requested services have been resolved and returned, Angular can call the component's constructor with those services as arguments.</p>\n<p>The process of <code>HeroService</code> injection looks something like this.</p>\n<div class=\"lightbox\">\n <img src=\"generated/images/guide/architecture/injector-injects.png\" alt=\"Service\" class=\"left\" width=\"479\" height=\"218\">\n</div>\n<h3 id=\"providing-services\">Providing services<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"guide/architecture-services#providing-services\"><i class=\"material-icons\">link</i></a></h3>\n<p>You must register at least one <em>provider</em> of any service you are going to use.\nThe provider can be part of the service's own metadata, making that service available everywhere,\nor you can register providers with specific modules or components.\nYou register providers in the metadata of the service (in the <code>@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()</code> decorator),\nor in the <code>@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>()</code> or <code>@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>()</code> metadata</p>\n<ul>\n<li>\n<p>By default, the Angular CLI command <a href=\"cli/generate\"><code>ng generate service</code></a> registers a provider with the root injector for your service by including provider metadata in the <code>@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()</code> decorator. The tutorial uses this method to register the provider of HeroService class definition.</p>\n<code-example>\n@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>({\n providedIn: 'root',\n})\n</code-example>\n<p> When you provide the service at the root level, Angular creates a single, shared instance of <code>HeroService</code>\nand injects it into any class that asks for it.\nRegistering the provider in the <code>@<a href=\"api/core/Injectable\" class=\"code-anchor\">Injectable</a>()</code> metadata also allows Angular to optimize an app\nby removing the service from the compiled app if it isn't used, a process known as <em>tree-shaking</em>.</p>\n</li>\n<li>\n<p>When you register a provider with a <a href=\"guide/architecture-modules\">specific NgModule</a>, the same instance of a service is available to all components in that NgModule. To register at this level, use the <code>providers</code> property of the <code>@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>()</code> decorator.</p>\n<code-example>\n@<a href=\"api/core/NgModule\" class=\"code-anchor\">NgModule</a>({\n providers: [\n BackendService,\n Logger\n ],\n ...\n})\n</code-example>\n</li>\n<li>\n<p>When you register a provider at the component level, you get a new instance of the\nservice with each new instance of that component.\nAt the component level, register a service provider in the <code>providers</code> property of the <code>@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>()</code> metadata.</p>\n<p> <code-example path=\"architecture/src/app/hero-list.component.ts\" header=\"src/app/hero-list.component.ts (component providers)\" region=\"providers\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'app-hero-list',\n templateUrl: './hero-list.component.html',\n providers: [ HeroService ]\n})\n\n</code-example></p>\n</li>\n</ul>\n<p>For more detailed information, see the <a href=\"guide/dependency-injection\">Dependency Injection</a> section.</p>\n\n \n</div>\n\n<!-- links to this doc:\n - api/core/Injectable\n - errors/NG0201\n - guide/architecture\n - guide/architecture-modules\n - guide/creating-libraries\n - guide/glossary\n - guide/hierarchical-dependency-injection\n - guide/lightweight-injection-tokens\n - start/start-data\n-->\n<!-- links from this doc:\n - api/common/http/HttpClient\n - api/core/Component\n - api/core/Injectable\n - api/core/NgModule\n - cli/generate\n - guide/architecture-modules\n - guide/architecture-services#dependency-injection-di\n - guide/architecture-services#introduction-to-services-and-dependency-injection\n - guide/architecture-services#providing-services\n - guide/architecture-services#service-examples\n - guide/dependency-injection\n - https://github.com/angular/angular/edit/master/aio/content/guide/architecture-services.md?message=docs%3A%20describe%20your%20change...\n-->"
}