Ionic2 定义全局变量方法

来源:互联网 发布:霍华德新秀体测数据 编辑:程序博客网 时间:2024/05/01 14:58

在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()即可获取设备高度。

1 0
原创粉丝点击