首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$ notation来访问基类的属性

$ notation来访问基类的属性
EN

Stack Overflow用户
提问于 2019-11-29 05:54:49
回答 1查看 24关注 0票数 0

我正在分享我的angular 8应用程序中的代码。正如你在下面看到的,有两个类。基本服务类和从基本服务类派生的子服务类。正如您还可以看到的,子类使用的是基类属性,如${this._baseUrl}。谁能告诉我这个符号${}是什么意思?

基础服务

代码语言:javascript
复制
@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;
    }
}

城市服务

代码语言:javascript
复制
@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}`;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-11-29 06:08:04

这方面的技术术语是字符串插值。

您的城市服务是基础服务的扩展。城市服务中的this._baseUrl是在基本服务中定义的,该服务是在您的环境中使用属性apiBaseUrl定义的。

总之,如果您的apiBaseUrl设置为值stackoverflow.com,则cities服务中的函数getAllCities()将返回stackoverflow.com/api/cities

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59096551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档