React native 我正在尝试导出此JSON数据以便在项目中导入,但是它显示了意外的语法错误
当它和组件在同一个文件中时,它工作得非常好。现在我试图将其从组件中排除,以使项目更干净。但是,它显示了意外的语法错误。文件格式为JSON。如何修复此代码并按需要工作React native 我正在尝试导出此JSON数据以便在项目中导入,但是它显示了意外的语法错误,react-native,react-native-android,react-native-ios,native-base,React Native,React Native Android,React Native Ios,Native Base,当它和组件在同一个文件中时,它工作得非常好。现在我试图将其从组件中排除,以使项目更干净。但是,它显示了意外的语法错误。文件格式为JSON。如何修复此代码并按需要工作 const postsData = [ { id: 1, title: "How to start a business with 100$", published: "14h ago", image: require("../img/img1.jpg")
const postsData = [
{
id: 1,
title: "How to start a business with 100$",
published: "14h ago",
image: require("../img/img1.jpg"),
},
{
id: 2,
title: "Get funding for your startup",
published: "19h ago",
image: require("../img/img2.jpg"),
},
{
id: 3,
title: "Latest Fashion Trends for 2018",
published: "14h ago",
image: require("../img/img3.jpg"),
},
]
export {postsData};
试试这个:
export default [
{
id: 1,
title: "How to start a business with 100$",
published: "14h ago",
image: require("../img/img1.jpg"),
},
{
id: 2,
title: "Get funding for your startup",
published: "19h ago",
image: require("../img/img2.jpg"),
},
{
id: 3,
title: "Latest Fashion Trends for 2018",
published: "14h ago",
image: require("../img/img3.jpg"),
}
]
和import
it like
从“文件路径”导入postsData代码>
PS:
postsData没有命名约定
您可以命名它
什么都行
- 您的
json
文件应该有一个扩展名.js
,用于您的案例,因为您没有使用json对象
您需要添加双“”标记
像下面这样
{
"id": "1",
"title": "How to start a business with 100$",
"published": "14h ago",
}
请使用json lint验证json-
请跟随
[{
"id": "1",
"title": "How to start a business with 100$",
"published": "14h ago",
"images": [{
"bannerImg1": "./images/effy.jpg"
},
{
"id": "2",
"title": "Get funding for your startup",
"published": "19h ago",
"images": [{
"bannerImg1": "./images/effy.jpg"
},
{
"id": "3",
"title": "Latest Fashion Trends for 2018",
"published": "14h ago",
"images": [{
"bannerImg1": "./images/effy.jpg"
}]
}
]
如果它是一个类似于MyFile.JSON的JSON文件,那么您不能使用当前代码从JSON文件导出任何内容,因为它包含类似于export或const的关键字,并且您不能在JSON文件中使用JavaScript关键字
您可以将文件扩展名更改为MyFile.js
或
您可以创建一个json文件,如MyFile.json,并只放置json代码
{
"id": "1",
"title": "How to start a business with 100$",
"published": "14h ago",
}
并从JS文件中要求它,如:
从“/MyFile.json”导入数据
这不是一个json,它只是一个javascript文件(根据语法)。
最后一行以export{name1,name2,…,nameN}的形式构造对象代码>其中name1
,name2
,…,nameN
是命名属性。
要命名属性,请使用以下语法:
import { export } from "module-name";
在你的情况下是这样的
import { postsData } from "<your file without js ext>"
从“”导入{postsData}
如上所述,这不是一个JSON,只是一个对象
首先,新建一个js文件,您可以随意命名该文件。例如,data.js
然后在data.js中这样写:
export defult{
postsData: [
{
id: 1,
title: "How to start a business with 100$",
published: "14h ago",
image: require("../img/img1.jpg"),
},
{
id: 2,
title: "Get funding for your startup",
published: "19h ago",
image: require("../img/img2.jpg"),
},
{
id: 3,
title: "Latest Fashion Trends for 2018",
published: "14h ago",
image: require("../img/img3.jpg"),
},
]
}
最后,在组件文件中:
import {xxxx} from './data';
通过这种方式,您可以获取数据,xxxx.postsData
如果您有.JSON文件,并且其中的所有内容都是JSON,那么您不需要像普通模块一样导出它。您只需将其导入其他模块,并像普通对象一样使用它。就这样 您编写了导出默认值[…..它如何知道它是否是postsData?在您的示例中,它没有定义。您可以为它命名任何名称。无需在JSON
文件中定义任何名称。您必须在使用import
时命名它。语法错误显示在该文件的第一行:export default[让我们来看看]。
with commonjs module
const express = require("express");
const config = require("./config.json");
typescript
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true
}
}
import law from './keywords/law.json'
import special from './keywords/special.json'