Umgebungsvariablen in Ionic 2

31.03.2017 Stefan Welsch
Mobile Tech ionic mad handson goodtoknow

Wenn Sie Environment Variablen in Ionic (2.2.0) verwenden möchten, können Sie es auf folgende Art und Weise versuchen.

/src/config/config.dev.ts and /src/config/config.prd.ts

export const ENV = {
    //0=debug, 1=info, 2=error
    LOG_LEVEL: 0,
    ...
}
package.json

"config": {
    "ionic_webpack": "./src/config/webpack.config.js"
}
/src/config/webpack.config.js

var path = require('path');
var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');

module.exports = function () {
    useDefaultConfig.resolve.alias = {
        "@app/config": path.resolve('./src/config/config.' + process.env.IONIC_ENV + '.ts')
    };

    return useDefaultConfig;
}

Wie wendet man es an?

...
import { ENV } from '@app/config'

@Component({
  selector: 'page-contact',
  templateUrl: 'contact.html'
})
export class Test {
	private envVar = ENV.LOG_LEVEL;
}

Stefan Welsch - pioneer, stuntman, mentor. As the founder of b-nova, Stefan is always looking for new and promising fields of development. He is a pragmatist through and through and therefore prefers to write articles that are as close as possible to real-world scenarios.