1

In my Angular2 application I use services which call a REST API like this from http://localhost:22222/app/webresources/entity..

I want to set part of this URL just one time and call it from the services which I need.
I think I need to create an Interface which has a constant URL, but is it possible to implement this in a service?

Timothy
  • 2,004
  • 3
  • 23
  • 29
Nour_92
  • 81
  • 1
  • 10

1 Answers1

1

I put something like this in my data-access.service.ts:

export const API_URL: string = "http://my.api.com/"

It's useful because I can use it in my service methods:

getStuff(): Observable<Stuff> {
    return this.http.get(API_URL + `/path/to/stuff/with/${parameters}`)
        .map(response => response.json())
        .catch(this.logError);

Or later in a template somewhere:

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;
…
}
wolfhoundjesse
  • 1,085
  • 9
  • 32