相关文章推荐
跑龙套的大白菜  ·  VS2022 E1696 ...·  4 月前    · 
开朗的啄木鸟  ·  C++ AMP 概觀 | ...·  1 年前    · 

react 读取json文件

在 React 中读取 JSON 文件可以使用 JavaScript 的 fetch API 或 axios 库。

使用 fetch API:

fetch('path/to/file.json')
  .then(response => response.json())
  .then(data => {
    // Do something with the data

使用 axios:

import axios from 'axios';
axios.get('path/to/file.json')
  .then(response => {
    // Do something with the data
    const data = response.data;

这些代码片段都是在 componentDidMount() 或 useEffect() 等生命周期函数中使用。

注意:如果文件在本地,需要使用绝对路径,或者在服务器上设置代理。

  •