[英]Accessing base class properties in derived class using prototype/Access base class properties in the decorator
[英]$ notation to access properties of the base class class
我正在共享我的 angular 8 應用程序中的代碼。 正如您在下面看到的,有兩個類。 基服務類和派生自基服務類的子服務類。 正如您還可以看到的,子類正在使用 ${this._baseUrl} 這樣的基類屬性。 有人能告訴我這個符號是什么意思 ${} 嗎?
基地服務
@Injectable()
export class BaseService {
_baseUrl: string = environment.apiBaseUrl;
constructor(protected httpClient: HttpClient, protected _injector: Injector){}
protected getRequestHeaders(): { headers: HttpHeaders | { [header: string]: string | string[]; } } {
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': `application/json, text/plain, */*`,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,DELETE,OPTIONS'
});
return { headers: headers };
}
protected getBaseUrl() : string {
return this._baseUrl;
}
}
城市服務
@Injectable()
export class CitiesEndpoint extends BaseService {
constructor(_httpClient: HttpClient, _injector: Injector) {
super(_httpClient, _injector);
}
// city api endpoints
getAllCities() {
return `${this._baseUrl}/api/cities`;
}
deleteCity(id) {
return `${this._baseUrl}/api/cities/delete-city//${id}`;
}
}
技術術語是字符串插值。
您的城市服務是基礎服務的延伸。 城市服務中的this._baseUrl
是在基礎服務中定義的,基礎服務是在您的環境中使用屬性apiBaseUrl
定義的。
總而言之,如果您的apiBaseUrl
設置了值stackoverflow.com
,您的城市服務中的函數getAllCities()
將返回stackoverflow.com/api/cities
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.