【问题标题】:call a global constant into service Angular2将全局常量调用到服务 Angular2
【发布时间】:2016-06-30 17:53:54
【问题描述】:
在我的 Angular2 应用程序中,我使用从 http://localhost:22222/app/webresources/entity.. 调用这样的 REST API 的服务
我只想设置此 URL 的一部分,然后从我需要的服务中调用它。
我想我需要创建一个具有恒定 URL 的接口,但是可以在服务中实现它吗?
【问题讨论】:
标签:
service
interface
angular
constants
【解决方案1】:
我在我的 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);
或者稍后在某个模板中:
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;
…
}