Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了typescript – 在Angular 2中定义全局常量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Angular1.x中,你可以这样定义常量
angular.module('mainApp.config',[])
.constant('API_ENDPOINT','http://127.0.0.1:6666/api/')

在Angular2(带打字稿)中是什么等价的?
我只是不想重复的API基本url一遍又一遍在我所有的服务。

下面的更改为我在Angular 2最终版本工作:
export class AppSetTings {
   public static API_ENDPOINT='http://127.0.0.1:6666/api/';
}

然后在服务:

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);
    }
}

大佬总结

以上是大佬教程为你收集整理的typescript – 在Angular 2中定义全局常量全部内容,希望文章能够帮你解决typescript – 在Angular 2中定义全局常量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。