Typescript Provider()在angular 2中做什么?
公司的具体作用是什么Typescript Provider()在angular 2中做什么?,typescript,angular,Typescript,Angular,公司的具体作用是什么 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'})]); 简而
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'})]);
简而言之,它创建了一个提供者。请参见下面代码的结尾:
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;
从“../facade/lang”导入{Type};
/**
*描述{@link Injector}应如何实例化给定令牌。
*
*请参阅{@link-provide}。
*
*####示例([现场演示](http://plnkr.co/edit/GNAyj6K6PfYg2NBzgwZ5?p%3Dpreview&p=preview))
*
*``` javascript
*var注入器=注入器.resolveAndCreate([
*新提供程序(“消息”{useValue:'Hello'})
* ]);
*
*expect(injector.get(“message”).toEqual(“Hello”);
* ```
*@ts2dart\u const
*@弃用
*/
导出声明类提供程序{
/**
*检索此提供程序时使用的令牌。通常,它是类型{@link type}。
*/
代币:任何;
/**
*将DI令牌绑定到实现类。
*
*####示例([现场演示](http://plnkr.co/edit/RSTG86qgmoxCyj9SWPwY?p=preview))
*
*由于'useExisting'和'useClass'经常混淆,因此示例包含
*这两个用例都便于比较。
*
*```打字脚本
*类别车辆{}
*
*汽车类扩展车辆{}
*
*var injectorClass=Injector.resolveAndCreate([
*汽车,
*{提供:车辆,使用类别:汽车}
* ]);
*var injectorAlias=Injector.resolveAndCreate([
*汽车,
*{提供:车辆,使用现有:车辆}
* ]);
*
*expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
*expect(injectorClass.get(Vehicle)instanceof Car).toBe(true);
*
*期望(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
*expect(injectorAlias.get(Vehicle)instanceof Car).toBe(true);
* ```
*/
使用类别:类型;
/**
*将DI标记绑定到值。
*
*####示例([现场演示](http://plnkr.co/edit/UFVsMVQIDe7l4waWziES?p=preview))
*
*``` javascript
*var注入器=注入器.resolveAndCreate([
*新提供程序(“消息”{useValue:'Hello'})
* ]);
*
*expect(injector.get(“message”).toEqual(“Hello”);
* ```
*/
使用价值:任何;
/**
*将DI令牌绑定到现有令牌。
*
*{@link Injector}返回与使用提供的令牌相同的实例。
*这与“useClass”相反,后者返回单独的“useClass”实例。
*
*####示例([现场演示](http://plnkr.co/edit/QsatsOJJ6P8T2fMe9gr8?p=preview))
*
*因为'useExisting'和'useClass'经常混淆,所以示例包含
*这两个用例都便于比较。
*
*```打字脚本
*类别车辆{}
*
*汽车类扩展车辆{}
*
*var injectorAlias=Injector.resolveAndCreate([
*汽车,
*{提供:车辆,使用现有:车辆}
* ]);
*var injectorClass=Injector.resolveAndCreate([
*汽车,
*{提供:车辆,使用类别:汽车}
* ]);
*
*期望(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);
* ```
*/
使用现有:任何;
/**
*将DI标记绑定到计算值的函数。
*
*####示例([现场演示](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
*
*```打字脚本
*var注入器=注入器.resolveAndCreate([
*{provide:Number,useFactory:()=>{return 1+2;}},
*新提供程序(字符串,{useFactory:(值)=>{return”值:“+value;},
*部门:[编号]})
* ]);
*
*expect(注射器获取(数量)).toEqual(3);
*expect(injector.get(String)).toEqual('Value:3');
* ```
*
*与依赖项一起使用。
*/
使用工厂:功能;
/**
*指定一组依赖项
*(作为“token”)应注入工厂函数。
*
*####示例([现场演示](http://plnkr.co/edit/Scoxy0pJNqKGAPZY1VVC?p=preview))
*
*```打字脚本
*var注入器=注入器.resolveAndCreate([
*{provide:Number,useFactory:()=>{return 1+2;}},
*新提供程序(字符串,{useFactory:(值)=>{return”值:“+value;},
*部门:[编号]})
* ]);
*
*expect(注射器获取(数量)).toEqual(3);
*expect(injector.get(String)).toEqual('Value:3');
* ```
*
*与“useFactory”结合使用。
*/
依赖项:对象[];
构造函数(标记:any,{useClass,useValue,useExisting,useFactory,deps,multi}:{
useClass?:类型;
使用价值?:任何;
使用现有?:任何;
使用工厂?:功能;
deps?:对象[];
多重?:布尔;
});
/**
*创建多个与同一令牌匹配的提供程序(多提供程序)。
*
*多个提供者用于创建可插拔服务,系统就是从这里来的
*使用一些默认提供程序,用户可以注册其他提供程序。
*默认提供程序和其他提供程序的组合将是
*用于驱动系统的行为。
*
*####示例
*
*```打字脚本
*var注入器=注入器.resolveAndCreate([
*新提供程序(“Strings”{useValue:“String1”,multi:true}),
*新提供程序(“Strings”,{useValue:“String2”,multi:true})
* ]);
*
*expect(injector.get(“Strings”).toEqual([“String1”、“String2”));
* ```
*
*不能混合使用多个提供程序和常规提供程序。请执行以下操作:
*将引发异常:
*
*```打字脚本
*var注入器=注入器.resolveAndCreate([
*新提供程序(“Strings”{useValue:“String1”,multi:true}),
*新提供程序(“Strings”,{useValue:“String2”})
* ]);