[英]what is the difference between @Inject(Injector) injector and injector?
[英]What is the difference between PROVIDER, INJECTOR and SERVICE in Angular 2-7?
有誰能請用簡單的詞語向我解釋這些差異。 任何有或沒有代碼的實時示例也可以。
服務是Angular中的一個類,它使用Angular依賴注入器注冊。 在下面的示例中,StudentService類是一項服務。
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class StudentService {
constructor() { }
}
Angular進程器負責創建服務實例並將它們注入類中。 通常注射器在幕后工作。 下面的代碼顯示了顯式創建的注入器
constructor(private injector: Injector) { }
以下代碼將服務直接注入主機組件。
injector.get(Service)
提供商告訴注入器如何創建服務。 如果沒有提供者,注入器將不知道它負責注入服務,也不能創建服務。 通常,在模塊或組件元數據中提及提供程序。 例如,如果組件想要調用服務“FileWriter”,則組件應在元數據中提及應該由注入器創建和注入此服務。
providers: [FileWriter]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.