【发布时间】:2020-12-29 18:58:35
【问题描述】:
我已经看到了一些看起来像旧的 Angular 示例,其中使用“@Inject”注释完成依赖注入...
import { Component, Inject } from '@angular/core';
import { ChatWidget } from '../components/chat-widget';
@Component({
selector: 'app-root',
template: `Encryption: {{ encryption }}`
})
export class AppComponent {
encryption = this.chatWidget.chatSocket.encryption;
constructor(@Inject(ChatWidget) private chatWidget) { }
}
在更高版本的 Angular (>= 7) 中,如果被注入的东西是用 @Injectable 注释的,是否仍然需要 @Inject,例如
@Injectable({
providedIn: 'root',
})
export class ChatWidget {
我想我要问的是 Angular 的更高版本,还有什么理由继续使用 @Inject?
【问题讨论】:
标签: angular dependency-injection service injectable