有人能给我举个例子吗?我不能让这件事起作用。
http://www.ember-cli.com/user-guide/#Environments
我在config/ environment . my文件中设置了一个环境变量。在路由操作中,我想访问这个变量。但ENV还没有定义。
environment.js
if (environment === 'test') {
ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
}
if (environment === 'production') {
ENV.APP.REPORTING_SERVICE_URL = 'http://www.stackoverflow.com';
}路由
import ENV from 'my-demo-app/config/environment';
export default Ember.Route.extend({
actions: {
doSomething: function() {
console.log(ENV.REPORTING_SERVICE_URL); // ENV is undefined
}
}
});发布于 2015-09-17 09:41:11
请确保ENV在您的./config/environment.js中返回。
一般来说,它应该如下所示:
module.exports = function(environment) {
var ENV = {
modulePrefix: 'app',
environment: environment,
baseURL: '/',
locationType: 'auto',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
},
APP: {
// Here you can pass flags/options to your application instance when it is created
// Also put default values here
REPORTING_SERVICE_URL = 'http://www.stackoverflow.com'
}
};
// overwrite default values for different environments as needed
if (environment === 'development') {
}
if (environment === 'test') {
ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
}
if (environment === 'production') {
}
return ENV;
};然后,您可以在应用程序代码中使用它:
import ENV from './config/environment';
export default Ember.Route.extend({
actions: {
doSomething: function() {
console.log(ENV.APP.REPORTING_SERVICE_URL);
}
}
});发布于 2015-09-17 07:50:15
您是这样设置变量的:
ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';但以这种方式访问它:
ENV.REPORTING_SERVICE_URL您应该在设置时删除APP,或者在获取时添加它,例如:
ENV.APP.REPORTING_SERVICE_URL如果是undefined,请确保没有引入任何可能破坏环境的语法。基本上,config/environment.js应该是这样的:
module.exports = function(environment) {
var ENV = {
modulePrefix: "my-demo-app",
environment: environment,
baseURL: "/",
locationType: "auto"
};
return ENV;
};请注意,您导出的变量的名称(在本例中为ENV)不必与导入的变量名匹配。它应该导入文件中导出的所有内容,并将其放入您在import中指定的变量中。如果是undefined,则基本导出undefined。
https://stackoverflow.com/questions/32624540
复制相似问题