@Self
是 Angular 中的一个装饰器,它用于在注入器中查找依赖项。这意味着,当使用 @Self
来装饰依赖项时,注入器会在当前组件的构造函数中查找提供该依赖项的服务。这与使用 @Inject
注解时,注入器会在父组件中查找提供该依赖项的服务不同。例如:
import { Inject, Self } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: './my-component.html',
})
export class MyComponent {
constructor(@Inject(SomeService) public someService: SomeService,
@Self() private selfService: SelfService) {
// ...
}
}
在这个例子中,
@Inject
装饰器用于注入一个名为 SomeService 的服务,它会在父组件中查找该服务。而 @Self
装饰器则用于注入一个名为 SelfService 的服务,它会在当前组件中查找该服务。