本文介绍了在建立过程中,在ember-cli应用程序中使用ENV值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据构建环境设置我的RESTAdapter主机。



我认为该值可以存储在 config / environment中。 js 如下:

  if(environment ==='development '){
ENV.API_ENDPOINT ='http:// localhost:8080';
}

if(environment ==='production'){
ENV.API_ENDPOINT ='http://api.myserver.com';
}

但我不确定如何将信息插入 / p>

解决方案

您可以在 config / environment.js

  // snip 
APP:{
//这里你可以将标志/选项传递给应用程序实例
//创建时
API_HOST:'http://192.168.1.37:3000'//默认设置
}
};

if(environment ==='development'){
ENV.APP.LOG_TRANSITIONS = true;
ENV.APP.API_HOST ='http://192.168.1.37:3000'; // override
}

然后可以使用其他文件中的设置,如下所示: / p>

  // app / adapters / application.js:
从ember-data导入DS;

导出默认DS.RESTAdapter.extend({
host:window.MyAppENV.APP.API_HOST
});

MyApp 替换为您的应用程序。 p>

您可以使用 ember --environment 选项切换到构建环境:

  ember服务 - 环境生产

  ember build  - 环境开发

我还没有看到是否有办法动态提供价值,但您可以根据需要提供尽可能多的环境。



更新:添加完整性,根据Weston的评论,记录此功能。


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:

if (environment === 'development') {
  ENV.API_ENDPOINT = 'http://localhost:8080';
}

if (environment === 'production') {
  ENV.API_ENDPOINT = 'http://api.myserver.com';
}

But I am unsure how to insert the information into adapter/application.js during the build process.

解决方案

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.

这篇关于在建立过程中,在ember-cli应用程序中使用ENV值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 17:04