究竟提供了什么角色?
import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app.component';
import {provide} from '@angular/core';
import 'rxjs/Rx';
bootstrap(AppComponent,[provide('SECURITY_KEY',{useValue:'123abc'})]);
答案 0 :(得分:0)
简短的回答,它创建了一个提供者。请参阅下面代码的结尾:
import { Type } from '../facade/lang';
/**
* Describes how the {@link Injector} should instantiate a given token.
*
* See {@link provide}.
*
* ### Example ([live demo](http://plnkr.co/edit/GNAyj6K6PfYg2NBzgwZ5?p%3Dpreview&p=preview))
*
* ```javascript
* var injector = Injector.resolveAndCreate([
* new Provider("message", { useValue: 'Hello' })
* ]);
*
* expect(injector.get("message")).toEqual('Hello');
* ```
* @ts2dart_const
* @deprecated
*/
export declare class Provider {
/**
* Token used when retrieving this provider. Usually, it is a type {@link Type}.
*/
token: any;
/**
* Binds a DI token to an implementation class.
*
* ### Example ([live demo](http://plnkr.co/edit/RSTG86qgmoxCyj9SWPwY?p=preview))
*
* Because `useExisting` and `useClass` are often confused, the example contains
* both use cases for easy comparison.
*
* ```typescript
* class Vehicle {}
*
* class Car extends Vehicle {}
*
* var injectorClass = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useClass: Car }
* ]);
* var injectorAlias = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useExisting: Car }
* ]);
*
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
*
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
* ```
*/
useClass: Type;
/**
* Binds a DI token to a value.
*
* ### Example ([live demo](http://plnkr.co/edit/UFVsMVQIDe7l4waWziES?p=preview))
*
* ```javascript
* var injector = Injector.resolveAndCreate([
* new Provider("message", { useValue: 'Hello' })
* ]);
*
* expect(injector.get("message")).toEqual('Hello');
* ```
*/
useValue: any;
/**
* Binds a DI token to an existing token.
*
* {@link Injector} returns the same instance as if the provided token was used.
* This is in contrast to `useClass` where a separate instance of `useClass` is returned.
*
* ### Example ([live demo](http://plnkr.co/edit/QsatsOJJ6P8T2fMe9gr8?p=preview))
*
* Because `useExisting` and `useClass` are often confused the example contains
* both use cases for easy comparison.
*
* ```typescript
* class Vehicle {}
*
* class Car extends Vehicle {}
*
* var injectorAlias = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useExisting: Car }
* ]);
* var injectorClass = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useClass: Car }
* ]);
*
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
*
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
* ```
*/
useExisting: any;
/**
* Binds a DI token to a function which computes the value.
*
* ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* {provide: Number, useFactory: () => { return 1+2; }},
* new Provider(String, { useFactory: (value) => { return "Value: " + value; },
* deps: [Number] })
* ]);
*
* expect(injector.get(Number)).toEqual(3);
* expect(injector.get(String)).toEqual('Value: 3');
* ```
*
* Used in conjunction with dependencies.
*/
useFactory: Function;
/**
* Specifies a set of dependencies
* (as `token`s) which should be injected into the factory function.
*
* ### Example ([live demo](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* {provide: Number, useFactory: () => { return 1+2; }},
* new Provider(String, { useFactory: (value) => { return "Value: " + value; },
* deps: [Number] })
* ]);
*
* expect(injector.get(Number)).toEqual(3);
* expect(injector.get(String)).toEqual('Value: 3');
* ```
*
* Used in conjunction with `useFactory`.
*/
dependencies: Object[];
constructor(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
useClass?: Type;
useValue?: any;
useExisting?: any;
useFactory?: Function;
deps?: Object[];
multi?: boolean;
});
/**
* Creates multiple providers matching the same token (a multi-provider).
*
* Multi-providers are used for creating pluggable service, where the system comes
* with some default providers, and the user can register additional providers.
* The combination of the default providers and the additional providers will be
* used to drive the behavior of the system.
*
* ### Example
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* new Provider("Strings", { useValue: "String1", multi: true}),
* new Provider("Strings", { useValue: "String2", multi: true})
* ]);
*
* expect(injector.get("Strings")).toEqual(["String1", "String2"]);
* ```
*
* Multi-providers and regular providers cannot be mixed. The following
* will throw an exception:
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* new Provider("Strings", { useValue: "String1", multi: true }),
* new Provider("Strings", { useValue: "String2"})
* ]);
* ```
*/
multi: boolean;
}
/**
* See {@link Provider} instead.
*
* @deprecated
* @ts2dart_const
*/
export declare class Binding extends Provider {
constructor(token: any, {toClass, toValue, toAlias, toFactory, deps, multi}: {
toClass?: Type;
toValue?: any;
toAlias?: any;
toFactory: Function;
deps?: Object[];
multi?: boolean;
});
/**
* @deprecated
*/
toClass: Type;
/**
* @deprecated
*/
toAlias: any;
/**
* @deprecated
*/
toFactory: Function;
/**
* @deprecated
*/
toValue: any;
}
/**
* Creates a {@link Provider}.
*
* To construct a {@link Provider}, bind a `token` to either a class, a value, a factory function,
* or
* to an existing `token`.
* See {@link ProviderBuilder} for more details.
*
* The `token` is most commonly a class or {@link OpaqueToken-class.html}.
*
* @deprecated
*/
export declare function bind(token: any): ProviderBuilder;
/**
* Helper class for the {@link bind} function.
* @deprecated
*/
export declare class ProviderBuilder {
token: any;
constructor(token: any);
/**
* Binds a DI token to a class.
*
* ### Example ([live demo](http://plnkr.co/edit/ZpBCSYqv6e2ud5KXLdxQ?p=preview))
*
* Because `toAlias` and `toClass` are often confused, the example contains
* both use cases for easy comparison.
*
* ```typescript
* class Vehicle {}
*
* class Car extends Vehicle {}
*
* var injectorClass = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useClass: Car}
* ]);
* var injectorAlias = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useExisting: Car}
* ]);
*
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
*
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
* ```
*/
toClass(type: Type): Provider;
/**
* Binds a DI token to a value.
*
* ### Example ([live demo](http://plnkr.co/edit/G024PFHmDL0cJFgfZK8O?p=preview))
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* {provide: 'message', useValue: 'Hello'}
* ]);
*
* expect(injector.get('message')).toEqual('Hello');
* ```
*/
toValue(value: any): Provider;
/**
* Binds a DI token to an existing token.
*
* Angular will return the same instance as if the provided token was used. (This is
* in contrast to `useClass` where a separate instance of `useClass` will be returned.)
*
* ### Example ([live demo](http://plnkr.co/edit/uBaoF2pN5cfc5AfZapNw?p=preview))
*
* Because `toAlias` and `toClass` are often confused, the example contains
* both use cases for easy comparison.
*
* ```typescript
* class Vehicle {}
*
* class Car extends Vehicle {}
*
* var injectorAlias = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useExisting: Car}
* ]);
* var injectorClass = Injector.resolveAndCreate([
* Car,
* {provide: Vehicle, useClass: Car})
* ]);
*
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
*
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
* ```
*/
toAlias(aliasToken: any): Provider;
/**
* Binds a DI token to a function which computes the value.
*
* ### Example ([live demo](http://plnkr.co/edit/OejNIfTT3zb1iBxaIYOb?p=preview))
*
* ```typescript
* var injector = Injector.resolveAndCreate([
* {provide: Number, useFactory: () => { return 1+2; }},
* {provide: String, useFactory: (v) => { return "Value: " + v; }, deps: [Number]}
* ]);
*
* expect(injector.get(Number)).toEqual(3);
* expect(injector.get(String)).toEqual('Value: 3');
* ```
*/
toFactory(factory: Function, dependencies?: any[]): Provider;
}
/**
* Creates a {@link Provider}.
*
* See {@link Provider} for more details.
*
* <!-- TODO: improve the docs -->
* @deprecated
*/
export declare function provide(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
useClass?: Type;
useValue?: any;
useExisting?: any;
useFactory?: Function;
deps?: Object[];
multi?: boolean;
}): Provider;
答案 1 :(得分:0)
这是提供者的定义:
注射器供应商
提供程序提供依赖项的具体运行时版本 值。注入器依赖于提供者来创建实例 注入器注入组件和其他服务的服务。
我们必须使用注射器注册服务提供者,否则注册 知道如何创建服务。
我们每个组件有一个注射器,如下所述:
每个组件实例都有自己的注入器和一个注入器 level是树中它上面的注入器的子注入器。
当底部的组件请求依赖时,Angular会尝试 满足与该组件中注册的提供者的依赖关系 自己的注射器如果组件的注入器缺少提供者,那么 将请求传递给其父组件的注入器。如果说 注入器无法满足请求,它将它传递给它的父节点 组件的注射器。这些要求一直在冒泡,直到找到 可以处理请求或用完组件的注入器 祖先。如果我们用尽祖先,Angular会抛出错误。
在这些情况下,指示“顶级”注入器它将能够提供这些类型的实例(或在这种情况下的值)。然后结束:提供者配置一个注射器,每个组件有一个注射器。在这种情况下,您正在配置AppComponent以提供SECURITY_KEY的值。
现在你想知道:为什么?,为什么我需要编写这个(显然)样板代码?:问题是你可以用这样的方式(用工厂)配置一个注入器,它将返回一个符合的实例到界面但可以让你改变特定的行为。你还记得Car
与Tesla
,Volkswagen
等实现的接口示例吗?是这么简单。
来源:
https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injector-providers