在ionic2中沒有提供像ionic1中的constant那樣的方法去管理全局變量。但是在ionic2中可以通過以下方式進行全局變量的管理:
在app目錄下新建app.config.ts文件,并新建類AppConfig,在類里面創(chuàng)建靜態(tài)方法
export class AppConfig {
//測試環(huán)境URL
public static getDebugUrl() {
return "http://localhost:8080";
}
//生產環(huán)境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()即可獲取設備高度。