我正在分享我的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}`;
}
}发布于 2019-11-29 06:08:04
这方面的技术术语是字符串插值。
您的城市服务是基础服务的扩展。城市服务中的this._baseUrl是在基本服务中定义的,该服务是在您的环境中使用属性apiBaseUrl定义的。
总之,如果您的apiBaseUrl设置为值stackoverflow.com,则cities服务中的函数getAllCities()将返回stackoverflow.com/api/cities
https://stackoverflow.com/questions/59096551
复制相似问题