The easiest way would be to just define and export each variable independently. This way you can also import them independently as well.
export const rooturl = 'http://127.0.0.1:8000';
export const cityListUrl = rooturl + '/api/city/'
And import them this way.
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {cityListUrl} from 'app/backendUrls';
@Injectable()
export class cityGetService{
constructor(private http: Http){}
cityGet(){
this.http.get(cityListUrl)
}
}
If you need them all together in an object just export them like that as well.
export const rooturl = 'http://127.0.0.1:8000';
export const cityListUrl = rooturl + '/api/city/'
export const all = {
rooturl, cityListUrl
}
The way you have it now it's a class, that must be instantiated in order to get access to it's instance properties.
The generated code for your class looks like this.
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var backendUrls = (function () {
function backendUrls() {
// root url
this.rooturl = 'http://127.0.0.1:8000';
//get a list of all cities
this.cityListUrl = this.rooturl + '/api/city/';
}
return backendUrls;
}());
exports.backendUrls = backendUrls;
});
If you need a class you will first have to make an instance of it using the new keyword.
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {backendUrls} from 'app/backendUrls';
@Injectable()
export class cityGetService{
constructor(private http: Http){}
cityGet(){
this.http.get(new backendUrls().cityListUrl)
}
}
If you need to use a class but want to have this in a static manner you can make the properties static then they'll get defined as properties on the class itself instead of an instance.
export class backendUrls {
// root url
static rooturl: string = 'http://127.0.0.1:8000';
// need to use the class to access the root since we don't have an instance.
static cityListUrl: string = backendUrls.rooturl + '/api/city/';
}