填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

forwardRef

允許參考尚未定義的參考。

Allows to refer to references which are not yet defined.

檢視"說明"...

forwardRef(forwardRefFn: ForwardRefFn): Type<any>
      
      forwardRef(forwardRefFn: ForwardRefFn): Type<any>
    
引數
forwardRefFn ForwardRefFn
返回值

Type<any>

說明

例如,當我們需要為所宣告的 DI 而參考此 token,但尚未定義該令牌時,將使用 forwardRef。當我們建立尚未定義的查詢的 token 時,也會使用它。

For instance, forwardRef is used when the token which we need to refer to for the purposes of DI is declared, but not yet defined. It is also used when the token which we use when creating a query is not yet defined.

使用說明

例子

Example

class Door { lock: Lock; // Door attempts to inject Lock, despite it not being defined yet. // forwardRef makes this possible. constructor(@Inject(forwardRef(() => Lock)) lock: Lock) { this.lock = lock; } } // Only at this point Lock is defined. class Lock {} const injector = Injector.create({providers: [{provide: Lock, deps: []}, {provide: Door, deps: [Lock]}]}); expect(injector.get(Door) instanceof Door).toBe(true); expect(injector.get(Door).lock instanceof Lock).toBe(true);
      
      class Door {
  lock: Lock;

  // Door attempts to inject Lock, despite it not being defined yet.
  // forwardRef makes this possible.
  constructor(@Inject(forwardRef(() => Lock)) lock: Lock) {
    this.lock = lock;
  }
}

// Only at this point Lock is defined.
class Lock {}

const injector =
    Injector.create({providers: [{provide: Lock, deps: []}, {provide: Door, deps: [Lock]}]});

expect(injector.get(Door) instanceof Door).toBe(true);
expect(injector.get(Door).lock instanceof Lock).toBe(true);