【问题标题】:how to fetch data from json file using vite reactjs如何使用 vite reactjs 从 json 文件中获取数据
【发布时间】:2022-06-30 15:06:50
【问题描述】:

注意

用于创建 reactjs-app 的 vitejs 构建工具。

问题

我可以在开发服务器中读取 data.json,但我无法从生产中读取 data.json 我该如何解决这个问题。

File Tree

获取data.json的代码

    fetch('data/data.json').then(res => res.json()).then(d => {
        setNet({...net, isLoading: !net.isLoading})
        setData(d);
    }).catch(err => setNet({...net, msg: <p>Some thing went <span className="err__wrong">wrong</span> please try again after some time....</p>}))
  }, [])

如果我在 dist 文件中使用 npm run build 构建 reactjs,则没有 data.json 文件。

谁能解决这个问题?

Here is the project repo

【问题讨论】:

    标签: reactjs json fetch vite


    【解决方案1】:

    您应该在根级别创建一个公共文件夹并将您的 json 文件放在那里,以便在构建后它将位于 dist 文件夹中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2014-09-06
      • 2021-05-22
      • 1970-01-01
      相关资源
      最近更新 更多