我使用 create-react-app 作为我的 React 应用程序的样板。
我正在使用 fetch 向本地服务器发出请求
fetch("http://localhost:3000/users")
.then(function(res){
return res.json()
})
.then(function(res){
return res.data
})
Run Code Online (Sandbox Code Playgroud)
最终,当我部署到heroku时,我不会使用http://localhost,而是使用类似https://safe-escarpment-99271.herokuapp.com/的东西。
有没有办法将 API url 存储在环境变量中
IE。fetch(API_URL)
所以我可以在部署时将其放在 Github 上,但仍然可以在本地进行测试,而不必来回更改 url。
我懂了
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV': JSON.stringify('production'),
'API_URL': JSON.stringify('http://localhost:8080/bands')
}
}),
Run Code Online (Sandbox Code Playgroud)
很多答案,但这似乎对我不起作用。当我console.log(process.env)唯一表现出NODE_ENV的是PUBLIC_URL