反应原生'需要'陷阱?适用于本地映像文件,但不适用于同一目录中的csv文件 [英] React Native 'require' gotcha? Works for local image file, but not csv file in same directory
问题描述
这里有一个React Native新手。
a React Native newbie here.
在我的组件内部(在App.js文件中),我正在声明一个变量并将其设置为本地CSV文件,使用 require()
。
Inside my component (inside by App.js file), I'm declaring a variable and setting it to a local CSV file, using require( )
.
此错误--->
componentWillMount() {
var csvFilePath = require('./assets/imagesdata.csv');
但是,当我将其设置为我想要的CSV时,它会给我一个错误:
However, when I set this to the CSV I want, it gives me an error:
Unable to resolve ./assets/imagesdata.csv" from ".//App.js`: The module `./assets/imagesdata.csv` could not be found"
奇怪的是:如果我只是将CSV输出在同一目录中的另一个文件的文件,让我们使用.png图像文件,它没有错误工作正常 - 给出了什么?
The curious thing is this: if I just sub out the CSV file for another file in the same directory, let's use a .png image file, it works fine with no error -- what gives?
没有错误(.png中的子文件在SAME子目录中作为.csv文件无效的文件)--->
No error (subbed in .png file in the SAME subdirectory as the .csv file that didn't work) --->
componentWillMount() {
var csvFilePath = require('./assets/locationlogo.png');
是否有语法特定于我在React中不熟悉的CSV导入?
is there syntax specific to CSV imports that I am unfamiliar with in React?
这是我的目录的样子,以便您可以看到有问题的CSV文件(imagesdata.csv)事实上,与.png文件一样,在 assets /
的同一子目录中。这工作(上面那个例子中的locationlogo.png)。
Here is what my directory looks like, so that you can see that the CSV file in question (imagesdata.csv) is in fact in the same exact subdirectory of assets/
as the .png files that work (locationlogo.png in that example above) .
谢谢!
FYI:我已经google了一下并尝试了这里提出的解决方案(可能不适用于我的情况,但我很绝望:< a href =https://github.com/oblador/react-native-vector-icons/issues/626\"rel =nofollow noreferrer> https://github.com/oblador/react-native-vector-icons / issues / 626
FYI: I've googled around and tried the solution proposed here (probably not applicable to my case, but i'm desperate: https://github.com/oblador/react-native-vector-icons/issues/626
这建议运行 rm ./node_modules/react-native/local-cli/core/__fixtures__/files /package.json
在构建打包器之前,但是我做了这个,但仍然没有解决我的奇怪错误。)
This suggested running rm ./node_modules/react-native/local-cli/core/__fixtures__/files/package.json
before building packager, but I did this, and still didn't solve my weird error).
推荐答案
另一个React Native新手在这里,但我知道你面临的问题。
Another React Native newbie here but I know the problem you are facing.
React Native允许你读取图像,JSON文件和其他一些东西但不是CSV文件。要加载此CSV文件,您需要访问正在使用的平台(Android / iOS)的FS。
React Native allows you to read images, JSON files and a few other things but not CSV files. To get this CSV file loaded you need to access the FS of the platform(Android/iOS) you are using.
实现此目的的一种方法是使用反应天然-FS 。要加载CSV文件,我们可以使用以下代码:
One way to achieve this is to use react-native-fs. To load the CSV file we can use the following code:
var RNFS = require('react-native-fs');
RNFS.readFile("imagesdata.csv")
.then((result) => {
console.log('file contents', result);
})
.catch((err) => {
console.log(err.message, err.code);
});
要使用的路径与文件所在的位置不同。请参阅react-native-fs文档以获取更多信息。
The path to be used will differ to where the file is located. Kindly refer to the react-native-fs documentation for more information.
这篇关于反应原生'需要'陷阱?适用于本地映像文件,但不适用于同一目录中的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!