反应轴从不同的URL获取,然后设置在环境中
我正在尝试从我的React应用程序中设置“开发”和“生产”环境。 我做了一个.env.development.local
文件,然后添加了:
REACT_APP_API_URL = “http://localhost:3001”
REACT_APP_ENV = “dev”
然后在我的package.json.json
中我
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "cross-env REACT_APP_ENV='dev' npm run start"
},
现在在组件中添加,我正在尝试访问> react_app_api_url
const API_URI = process.env.REACT_APP_API_URL;
const fetchGuess = async () => {
const { data } = await axios.get(`${API_URI}/characters/random`);
if (data.success) {
setToGuess(data.data);
} else {
console.log("ERROR", data.errorMessage);
}
};
'
在响应中,我将其视为url axios试图从
http:// localhost:3000/%e2%80%9c 9c http:// localhost:3001%e2%e2%80%80%e2%80%e2%80% 9d/carame/Random
有关如何解决此问题的任何建议?
I'm trying to set a "development" and "production" environment from within my react app.
I made an .env.development.local
file and inside added:
REACT_APP_API_URL = “http://localhost:3001”
REACT_APP_ENV = “dev”
then in my package.json
I added
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "cross-env REACT_APP_ENV='dev' npm run start"
},
now in a component, I am trying to access the REACT_APP_API_URL
const API_URI = process.env.REACT_APP_API_URL;
const fetchGuess = async () => {
const { data } = await axios.get(`${API_URI}/characters/random`);
if (data.success) {
setToGuess(data.data);
} else {
console.log("ERROR", data.errorMessage);
}
};
'
In the response, I see this as the URL axios is trying to fetch from
http://localhost:3000 /%E2%80%9C http://localhost:3001%E2%80%9D/characters/random
Any advice on how to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论