我对使用react真的很陌生,我使用IntelliJ作为我的IDE。
我正在尝试在我的网站上显示JSON数据文件的内容,但是我收到错误消息说我创建了非法的进口减速。
import PostData from "./JSONData.js";
我要在课堂上加载信息的班级是
class Invoice extends React.Component
{
render()
{
return (
<div className ="container">
<h2>Allstate NI Graduate Application</h2>
<h2>Invoice</h2>
{PostData.map((postDetail, index)=>{
return <h1>{postDetail.code}</h1>
})}
</div>
);
}
}
这是我的主要HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gerry Byrne and David Wilson - ReactJS Tutorials</title>
<img src="../Drawable/carImage.png" id="carImg">
<!--Bootstap CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"
crossorigin="anonymous">
<!-- Tells the file to get the react.js file from a CDN.-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.0/react.js"></script>
<!--Tells the file to get the react-dom.js file from a CDN-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.0/react-dom.js"></script>
<!--The above two scripts used to be one but have since been seperated / ReactDom is only used to render ReactDOM.render()-->
<!---->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<!--Tells the file to get the compiler file from a CDN-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
<!---->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/2.5.2/ReactRouter.min.js"></script>
<link rel="stylesheet" href="../css/style.css" />
<!-- Latest compiled and minified CSS Bootstrap-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></link>
</head>
<body>
<div id="contentgoeshere"></div>
<br/>
<script type="text/jsx" src = "../JavaScript/ReactJSV3.js"></script>
</body>
</html>
我不确定我需要添加什么来允许我的项目导入这些文件。这是我得到的错误:
答案 0 :(得分:1)
此:
import PostData from "./JSONData.js";
是一个导入声明,用于导入模块的默认导出。 JSON数据文件不是JavaScript模块。
要读取数据文件,请使用平台上可用的任何机制(例如,网络上的fetch
)。
我应该注意,如果要将JSON数据文件设为JavaScript模块,则可能只需对其进行很小的更改。例如,如果您有:
{
"answer": 42,
"question": "Life, the Universe, and Everything!"
}
然后在开头添加“ export default”并在末尾添加;
,使其成为一个JavaScript模块,将该对象作为默认导出导出:
export default {
"answer": 42,
"question": "Life, the Universe, and Everything!"
};
例如export default /*...any valid JSON here...*/;
是具有默认导出功能的有效模块。
答案 1 :(得分:1)
非常简单。您只需导入.json
文件
import myJson from '../assets/my_json.json';
并直接使用myJson
my_json.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
}
}
]
}
OR
您可以导出对象(如果要使用.js文件)并导入另一个模块。
const myJson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
}
}
]
}
export default myJson;
导入声明
import myJson from '../assets/my_json.js';
答案 2 :(得分:0)
您不能像这样使用json。您需要fetch数据。如果您需要假数据,可以签出json-server。
处理数据的第二种方法:将数据放入.js文件并导出。
var dummy_data = {
name: 'John Smith',
imgURL: 'http://lorempixel.com/100/100/',
hobbyList: ['coding', 'writing', 'skiing']
}
export default dummy_data;
然后将json.js导入到您的app.js中并进行处理。
答案 3 :(得分:0)
如果您使用的是打字稿,则可以声明一个模块并将其作为对象使用
declare module "*.json" {
const value: any;
export default value;
}
//然后像这样食用
import * as de_trans from './translations/de.json';