在1.你可以这样定义常数:
angular.module('mainApp.config', [])
.constant('API_ENDPOINT', 'http://127.0.0.1:6666/api/')
在Angular(带TypeScript)中,什么是等效的?
我只是不想在我所有的服务中一遍又一遍地重复API基url.
在1.你可以这样定义常数:
angular.module('mainApp.config', [])
.constant('API_ENDPOINT', 'http://127.0.0.1:6666/api/')
在Angular(带TypeScript)中,什么是等效的?
我只是不想在我所有的服务中一遍又一遍地重复API基url.
Below changes works for me on Angular 2 final version:
export class AppSettings {
public static API_ENDPOINT='http://127.0.0.1:6666/api/';
}
And then in the service:
import {Http} from 'angular2/http';
import {Message} from '../models/message';
import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import {AppSettings} from '../appSettings';
import 'rxjs/add/operator/map';
@Injectable()
export class MessageService {
constructor(private http: Http) { }
getMessages(): Observable<Message[]> {
return this.http.get(AppSettings.API_ENDPOINT+'/messages')
.map(response => response.json())
.map((messages: Object[]) => {
return messages.map(message => this.parseData(message));
});
}
private parseData(data): Message {
return new Message(data);
}
}