[英]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.