Ember 构建后的配置文件

Ember config file after build

我想从配置文件而不是从 index.html(而不是从 config/environment 生成的元数据)中获取一些配置属性,例如 myConfig.js 有两个变量(lang 和主机)可以在构建后更改。目前我把那个变量放在 config/enviroment 中,但我想把这个变量和这个数据分开。

例如:

index.html:

<!DOCTYPE html>
   ...
   <meta name="myapp/config/environment" content="%7B%22modulePrefix%22%3A%22user%22%2C%22environment%22%3A%22development%22%2C%22baseURL%22%3A%22/%22%2C%22locationType%22%3A%22auto%22%2C%22contentSecurityPolicy%22%3A%7B%22default-src%22%3A%22%27none%27%20localhost%22%2C%22script-src%22%3A%22%27self%27%20%27exportApplicationGlobal%22%3Atrue%7D">
   ...
   <script src="myconfig.js" ></script>
   ...
</html>

myconfig.js:

module.exports = function() {

   var MYCONFIG = {
       lang: 'en',
       host: 'http://.....'
   }

   return MYCONFIG ;
};

如何做到这一点?

任何帮助将不胜感激

您可以创建 config 实用程序

// utils/config-utils.js
export default {
 lang: 'en',
 host: 'http://.....'
}; 

然后导入到你需要的地方:

// controllers/my-controller.js
import ConfigUtil from '../utils/config';

const {
 lang
} = ConfigUtil;

export default MyController.extend({
   ...
});

最后我在public文件夹下放了一个json文件,然后在app路径中使用Ember.$.ajax获取。