Angular 2 useExisting providers

后端 未结 3 1456
旧巷少年郎
旧巷少年郎 2020-12-31 04:41

What are the usages for useExisting provider?

Is it useExistingOrThrowIfThereIsNone or useExistingOrCreateIfThereIsNone? Can o

3条回答
  •  伪装坚强ぢ
    2020-12-31 05:03

    With this example

    providers: [
        A, 
        {provide: B, useClass: A}, 
        {provide: C, useExisting: A}]
    

    If you have

    constructor(private a: A)
    

    an instance for the first provider is created.

    constructor(private b: B)
    

    an instance for the 2nd provider is created

    constructor(private c: C)
    

    the instance of the first provider is injected.

    If you start fresh with

    constructor(private c: C)
    

    an instance for the first provider is created and injected

提交回复
热议问题