Service 将全局常量调用为服务变量2

Service 将全局常量调用为服务变量2,service,interface,angular,constants,Service,Interface,Angular,Constants,在我的Angular2应用程序中,我使用从http://localhost:22222/app/webresources/entity.. 我只想设置一次此URL的一部分,然后从我需要的服务调用它。 我想我需要创建一个具有恒定URL的接口,但是有可能在服务中实现它吗 我在data-access.service.ts中放了类似的内容: export const API_URL: string = "http://my.api.com/" 它很有用,因为我可以在我的服务方法中使用它: getStu

在我的Angular2应用程序中,我使用从
http://localhost:22222/app/webresources/entity..

我只想设置一次此URL的一部分,然后从我需要的服务调用它。

我想我需要创建一个具有恒定URL的接口,但是有可能在服务中实现它吗

我在data-access.service.ts中放了类似的内容:

export const API_URL: string = "http://my.api.com/"
它很有用,因为我可以在我的服务方法中使用它:

getStuff(): Observable<Stuff> {
    return this.http.get(API_URL + `/path/to/stuff/with/${parameters}`)
        .map(response => response.json())
        .catch(this.logError);
getStuff():可观察{
返回此.http.get(API_URL+`/path/to/stuff/with/${parameters}`)
.map(response=>response.json())
.catch(this.logError);
或者稍后在模板中的某个位置:

import { API_URL } from '../shared/data-access.service';

@Component({
    template: '<a href="{{api}}/stuff">Link to stuff</a>'
})
export class MyComponent {
    api: string = API_URL;
…
}
从“../shared/data access.service”导入{API_URL};
@组成部分({
模板:“”
})
导出类MyComponent{
api:string=api\u URL;
…
}

这篇文章应该对你有所帮助。谢谢你,这正是我想要的。我现在就试试