【发布时间】:2018-01-15 16:32:44
【问题描述】:
我知道这是一个非常常见的问题,并且我已经看到它在多个站点上被多次回答,但我无法让它工作问题是我正在消耗和 api 用于反应组件(我是使用 typescript 2.4.1 和 webpack 2.5.1):
....
fetch("some/url/api/", method:"POST",...).then(/*some code*/)
我想自动检测应用在哪个环境中,并根据它是 prod 还是 dev 来使用正确的 api。
通过网络搜索,我可以设法将它们放在 webpack.config 中:
...
var API_URL = {
production: JSON.stringify('http://some.server.example'),
development: JSON.stringify('http://localhost:xxxx')
}
var environment = process.env.NODE_ENV === 'production'?'production':'development'
...
const sharedConfig = () =>({
...
plugins:[
...
new webpack.DefinePlugin({
'API_URL':API_URL[environment]
})
]
});
通过这样调用 fetch:
fetch(API_URL+"api/endpoint")。
我收到错误消息:
Cannot find name 'API_URL'.any
我是否应该导入特定的东西来获取 API_URL 值。我认为 API_URL 将是全球可访问的。任何帮助将不胜感激。
【问题讨论】:
标签: typescript webpack javascriptservices