我正在尝试将静态JSON数据加载到我的反应应用程序 . 但是,它不允许我加载数据 .
我使用的是webpack版本4.26.1
它向我显示以下错误:
SyntaxError: src/data/movieData.json: Unexpected token, expected ; (2:10)
1 | {
2 | "data": [
| ^
3 | {
4 | "id": 1,
5 | "title": "Freed",
我的代码:
data/jsonResponse.json
{
"data": [
{
"id": 1,
"title": "Freed"
},
{
"id": 2,
"title": "Fifty"
}
]
}
main.js
import React, { Component } from 'react';
import Content from './Content';
import jsonResponse from './data/jsonResponse.json';
class Main extends Component {
render() {
return (
);
}
}
export default Main;
Content.js
import React from 'react';
const Content = () => {
const movies = this.props.item.data;
return (
movies.map(movie => {
return (
{movie.title}
);
})
)
}
export default Content;
Edited:
如果我使用js代替JSON,如:
const movies_data = {
"data": [
{
"id": 1,
"title": "Freed"
},
{
"id": 2,
"title": "Fifty"
}
]
}
export default movies_data;
并在Main.js文件中
import jsonResponse from './data/movieData';
然后在浏览器中显示以下错误 .
无法读取undefined的属性'props'