反应轴从不同的URL获取,然后设置在环境中

发布于 2025-02-09 23:30:03 字数 1038 浏览 2 评论 0原文

我正在尝试从我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文