美文网首页Ionic 2 花瓣 ..
Ionic2 定义全局变量方法

Ionic2 定义全局变量方法

作者: 待花谢花开 | 来源:发表于2017-06-15 08:59 被阅读143次

在ionic2中没有提供像ionic1中的constant那样的方法去管理全局变量。但是在ionic2中可以通过以下方式进行全局变量的管理:
在app目录下新建app.config.ts文件,并新建类AppConfig,在类里面创建静态方法

export class AppConfig {
    //测试环境URL
    public static getDebugUrl() {
        return "http://localhost:8080";
    }
    //生产环境URL
    public static getProdUrl() {
        return "http://service:8080";
    }
    //获取设备高度
    public static getWindowHeight() {
        return window.screen.height;
    }
    //获取设备宽度
    public static getWindowWidth() {
        return window.screen.width;
    }
}

然后再需要使用全局变量的地方导入AppConfig

import { AppConfig } from './../../app/app.config';

最后通过AppConfig.getWindowHeight()即可获取设备高度。

相关文章

网友评论

    本文标题:Ionic2 定义全局变量方法

    本文链接:https://www.haomeiwen.com/subject/bgryqxtx.html