文档forwardRef

forwardRef

在下面的例子当中 Person 先于 Father 声明,但是又依赖于 Father,这种情况下,你需要使用 forwardRef 来封装一次 Father,否则 redi 在记录依赖关系时, Father 的值会是 undefined。如果这种情况确实发生了,redi 会报错。

import { Self, SkipSelf } from "@wendellhu/redi";
 
class Person {
  constructor(
    @Self() @Inject(forwardRef(() => Father)) private readonly father: Father,
    @SkipSelf()
    @Inject(forwardRef(() => Father))
    private readonly grandfather: Father,
  ) {}
}
 
class Man extends Person {}
class Father extends Man {
  changeDiaper(): void {}
}
💡

这和 TypeScript 如何编译 class 有关。在装饰器执行时,Father 类所对应的标识符的值是 undefined