Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

javascript - $表示法访问基类的属性($ 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}`;
    }
}
  ask by Tom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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属性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()服务中的函数getAllCities()将返回stackoverflow.com/api/cities)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...