Using ENV values in ember-cli app during build

后端 未结 1 1321
北恋
北恋 2021-02-04 08:59

I would like to set my RESTAdapter host based on the build environment.

I assume the value can be stored in config/environment.js like this:



        
相关标签:
1条回答
  • 2021-02-04 09:21

    You define the setting like this in your config/environment.js:

      // snip
      APP: {
        // Here you can pass flags/options to your application instance
        // when it is created
        API_HOST: 'http://192.168.1.37:3000' // default setting
      }
    };
    
    if (environment === 'development') {
      ENV.APP.LOG_TRANSITIONS = true;
      ENV.APP.API_HOST = 'http://192.168.1.37:3000'; // override
    }
    

    You can then use the setting in other files like this:

    // app/adapters/application.js:
    import DS from "ember-data";
    
    export default DS.RESTAdapter.extend({
       host: window.MyAppENV.APP.API_HOST
    });
    

    Replace MyApp with your application.

    You switch to a build environment with the ember --environment option:

    ember serve --environment production
    

    or

    ember build --environment development
    

    I've not seen yet whether there is a way to provide the value dynamically, but you can provide as many environments as you want of course.

    Update: Adding for completeness, and as per Weston's comment, Environments documents this functionality.

    0 讨论(0)
提交回复
热议问题