【发布时间】:2019-09-23 21:42:11
【问题描述】:
我正在使用 react 并致力于在组件中获取 json 文件。
我想使用 fetch 获取 input.json 文件,但它不是控制台中的数据。为什么会这样?如何获取数据?
输入.json
{
"formId": 1,
"title": "from",
"items": [{
"itemId": 1,
"title": "style",
"formType": 1,
"options": [{
"id": 1,
"text": "one"
}, {
"id": 2,
"text": "two"
}, {
"id": 3,
"text": "three"
}]
}
App.js
import React from 'react';
import './App.css';
import inputData from './assets/input.json';
function App() {
const getData = () => {
fetch(inputData).then(response => {
return response;
}).then(data => {
console.log(data);
}).catch(err => {
console.log(err)
});
}
getData();
return (
<div className="App">
<h2>Fetch</h2>
</div>
);
}
export default App;
【问题讨论】:
-
请分享您的所有代码
-
您如何将
input.json文件中的json 导出为default导出? -
我会检查的。问题似乎是没有导出 json 文件。谢谢你的回答。
标签: fetch