【问题标题】:How to read .json file in React Native?如何在 React Native 中读取 .json 文件?
【发布时间】:2020-06-30 13:51:02
【问题描述】:

我正在使用 React Native 版本 0.59.4。 我创建了一个文件 data.json

但是当我像这样导入这个文件时

import myData from '../staticData/data.json';

我收到此错误Bundle failed: Could not resolve module ...,我确保路径正确

我也尝试删除扩展但没有改变

我在这里尝试了几乎所有的解决方案。

还有什么我可以尝试的吗?

编辑 我尝试将扩展名从 .json 更改为 .js 并在该文件中添加了 export default { // json },它就像一个魅力。

【问题讨论】:

  • 你可以尝试使用 const myData = require('../staticData/data.json');

标签: react-native react-native-android react-native-ios


【解决方案1】:

嗯,你做得对,我认为你在弄乱路径。 您确定这是正确的路径吗?您的文件夹组织得如何???? 另一种方法是使用旧的 JS 方式

const customData = require('./customData.json');

【讨论】:

  • 谢谢,react native 好像不支持 .json 扩展名
  • 看我很确定它支持它,但我不知道你正在使用你的 npm 版本的系统等等。我想你可以看看这里stackoverflow.com/questions/29452822/…,并有一些见解。
  • 谢谢我去看看
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2023-03-30
  • 2022-10-15
  • 2019-03-13
  • 1970-01-01
相关资源
最近更新 更多