像 Angular2 同樣,但願 ionic 能夠提供 2 個文件 ( environment.dev.ts 和 environment.prod.ts ),其中包含與開發和生產環境相對應的不一樣值的變量。在構建過程當中,要在應用程序中綁定適當的文件。android
// src/config/environment.dev.ts
export const ENV = {
APP_SERVE_URL: 'http://dev.example.com/api'
};
// src/config/environment.prod.ts
export const ENV = {
APP_SERVE_URL: 'http://prod.example.com/api'
};
複製代碼
// src/config/webpack.config.js
var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');
module.exports = function () {
useDefaultConfig[process.env.IONIC_ENV].resolve.alias = {
'@environment': path.resolve(__dirname + '/../../src/config/environment.' + process.env.IONIC_ENV + '.ts'),
};
return useDefaultConfig;
};
複製代碼
"config": {
"ionic_webpack": "./src/config/webpack.config.js"
}
複製代碼
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@environment": ["config/environment.prod"]
}
}
}
複製代碼
import { ENV } from '@environment';
webpack
ionic serve
或 cordova run android
ionic cordova build android --prod