Correct way to access current application configuration

前端 未结 3 726
一整个雨季
一整个雨季 2021-02-05 10:39

I added some configurations to myapp/config/environment:

if (environment === \'development\') {
  ENV.APP.AuthURL = \'http://localhost:5000/\';
}
         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 11:08

    There are a couple modern ways, as of this writing, when trying to access it from your application:

    1. import ENV from 'your-application-name/config/environment';
      • your-application-name should be what's in the modulePrefix key of config/environment.js and the name key of package.json
    2. Via Ember.getOwner(this).resolveRegistration('config:environment');

    Number one assumes you're using Ember CLI and is detailed in the ember docs under Configuring Your App:

    Ember CLI ships with support for managing your application's environment. Ember CLI will setup a default environment config file at config/environment. Here, you can define an ENV object for each environment, which are currently limited to three: development, test, and production.

    The ENV object contains three important keys:

    • EmberENV can be used to define Ember feature flags (see the Feature Flags guide).
    • APP can be used to pass flags/options to your application instance.
    • environment contains the name of the current environment (development,production or test).

    You can access these environment variables in your application code by importing from your-application-name/config/environment.

提交回复
热议问题