1 | /**
|
2 | * @license
|
3 | * Copyright Google Inc. All Rights Reserved.
|
4 | *
|
5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | * found in the LICENSE file at https://angular.io/license
|
7 | */
|
8 | import { __extends } from "tslib";
|
9 | /**
|
10 | * Creates a token that can be used in a DI Provider.
|
11 | *
|
12 | * ### Example ([live demo](http://plnkr.co/edit/Ys9ezXpj2Mnoy3Uc8KBp?p=preview))
|
13 | *
|
14 | * ```typescript
|
15 | * var t = new OpaqueToken("value");
|
16 | *
|
17 | * var injector = Injector.resolveAndCreate([
|
18 | * {provide: t, useValue: "bindingValue"}
|
19 | * ]);
|
20 | *
|
21 | * expect(injector.get(t)).toEqual("bindingValue");
|
22 | * ```
|
23 | *
|
24 | * Using an `OpaqueToken` is preferable to using strings as tokens because of possible collisions
|
25 | * caused by multiple providers using the same string as two different tokens.
|
26 | *
|
27 | * Using an `OpaqueToken` is preferable to using an `Object` as tokens because it provides better
|
28 | * error messages.
|
29 | * @deprecated since v4.0.0 because it does not support type information, use `InjectionToken<?>`
|
30 | * instead.
|
31 | */
|
32 | var OpaqueToken = /** @class */ (function () {
|
33 | function OpaqueToken(_desc) {
|
34 | this._desc = _desc;
|
35 | }
|
36 | OpaqueToken.prototype.toString = function () {
|
37 | return "Token " + this._desc;
|
38 | };
|
39 | return OpaqueToken;
|
40 | }());
|
41 | export { OpaqueToken };
|
42 | /**
|
43 | * Creates a token that can be used in a DI Provider.
|
44 | *
|
45 | * Use an `InjectionToken` whenever the type you are injecting is not reified (does not have a
|
46 | * runtime representation) such as when injecting an interface, callable type, array or
|
47 | * parametrized type.
|
48 | *
|
49 | * `InjectionToken` is parameterized on `T` which is the type of object which will be returned by
|
50 | * the `Injector`. This provides additional level of type safety.
|
51 | *
|
52 | * ```
|
53 | * interface MyInterface {...}
|
54 | * var myInterface = injector.get(new InjectionToken<MyInterface>('SomeToken'));
|
55 | * // myInterface is inferred to be MyInterface.
|
56 | * ```
|
57 | *
|
58 | * ### Example
|
59 | *
|
60 | * {@example core/di/ts/injector_spec.ts region='InjectionToken'}
|
61 | *
|
62 | * @stable
|
63 | */
|
64 | var InjectionToken = /** @class */ (function (_super) {
|
65 | __extends(InjectionToken, _super);
|
66 | function InjectionToken(desc) {
|
67 | return _super.call(this, desc) || this;
|
68 | }
|
69 | InjectionToken.prototype.toString = function () {
|
70 | return "InjectionToken " + this._desc;
|
71 | };
|
72 | return InjectionToken;
|
73 | }(OpaqueToken));
|
74 | export { InjectionToken };
|
75 | //# sourceMappingURL=injection_token.js.map |
\ | No newline at end of file |