简体   繁体   English

$ 符号访问基类类的属性

[英]$ notation to access properties of the base class class

I am sharing code from my angular 8 application.我正在共享我的 angular 8 应用程序中的代码。 As you can see below there are two classes.正如您在下面看到的,有两个类。 Base service class and child service class that derives from the base service class.基服务类和派生自基服务类的子服务类。 As you can also see the child class is using the base class property like this ${this._baseUrl}.正如您还可以看到的,子类正在使用 ${this._baseUrl} 这样的基类属性。 Could somebody tell me what does this notation mean ${}?有人能告诉我这个符号是什么意思 ${} 吗?

Base Service基地服务

@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;
    }
}

cities service城市服务

@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}`;
    }
}

Technical term for that is string interpolation.技术术语是字符串插值。

Your cities service is an extension of your base service.您的城市服务是基础服务的延伸。 And this._baseUrl in your cities service is defined in the base service, which is defined in your environment with property apiBaseUrl .城市服务中的this._baseUrl是在基础服务中定义的,基础服务是在您的环境中使用属性apiBaseUrl定义的。

To conclude, if your apiBaseUrl is set with value stackoverflow.com , your function getAllCities() in your cities service would return stackoverflow.com/api/cities总而言之,如果您的apiBaseUrl设置了值stackoverflow.com ,您的城市服务中的函数getAllCities()将返回stackoverflow.com/api/cities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM