简体   繁体   中英

Typescript compiler gives 'Unresolved variable or type' when I use a 'Token' in Angular2 provider() aliasing

Typscript can't seem to handle a 'Token' as used in a Angular2 provide() aliasing function. I'm wondering if there is a setting in the typescript compiler to fix that, or if I am stuck using a string type alias instead.

An example, as it would appear in main.ts bootstrapping function is as follows:

bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(**alias_token**, Child1aComponent)]);

Typescript (in webstorm, btw) reports an 'Unresolved variable or type alias_token' error.

There is an alternate 'provide' function that takes a string as param 1, but I'd prefer to use the Token version if possible.

Any ideas on this?

**alias_token** needs to be a valid type (that actually exists and is imported) or alternatively a string or an OpaqueToken

Some examples

class AliasToken {}

bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(AliasToken, {useClass  Child1aComponent})]);
...
constructor(private alias:AliasToken);
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide('alias_token', {useClass: Child1aComponent})]);
...
constructor(@Inject('alias_token') private alias:AliasToken);
var alias_token = new OpaqueToken("alias");
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(alias_token, {useClass: Child1aComponent})]);
...
constructor(@Inject(alias_token) private alias:AliasToken);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM