如何在ReactNative Project中将本地文本文件加载到字符串变量中? [英] How to load local text file into string variable in ReactNative Project?

查看:214
本文介绍了如何在ReactNative Project中将本地文本文件加载到字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ReactNative项目中有一些静态文本文件,我想加载到字符串变量中。像这样:

In my ReactNative project there are some static text files which i'd like to load into a string variable. Like this way:

var content = loadPlainTextFile("resources/tags.txt");
var tags = content.split("\n");

我尝试以同样的方式要求我需要javascript文件,但它不起作用,因为本机响应异常无法解析模块./data/tags.txt。

I tried require in the same way i'm requiring javascript files, but it doesn't work because react native gives exception "Unable to resolve module ./data/tags.txt".

var customData = require('./data/tags.txt');

我想require()无法处理纯文本文件?

I guess that require() is not able to handle plain text files?

如何做到正确?

推荐答案

创建一个如下所示的constants.js,你在哪里导出常量。

Create a constants.js like below, where you are exporting the constants.

module.exports = {
  SPRING_CONFIG: {tension: 40, friction: 3},
  COLOR: {
    COLOR1: '#ffffff',
    COLOR2: '#eeeeee',
    BLACK_TEXT: '#333',
  },
};

然后在您尝试导入的任何文件中,将其导入为...

Then in whatever file you are trying to import it, import it as...

import CONSTANTS from './constants'; //Make sure you put the right folder path.
console.log(CONSTANTS.COLOR.BLACK_TEXT);

这篇关于如何在ReactNative Project中将本地文本文件加载到字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆