[英]React Native fails to import files from folder above project folder
React noob here.在这里反应菜鸟。
I started with react and was able to do the basic.我从反应开始,并且能够做基本的。 When I wanted to make my code structured for future use, I failed.
当我想让我的代码结构化以备将来使用时,我失败了。
I have my project folder, and parallel to it I have a folder for all my common我有我的项目文件夹,与之平行的是我有一个文件夹,用于存放我所有常用的
| code:\
|- Code Folder\
|-- Project\
|--- App.js\
|--- screens\
|---- HomeScreen.js\
|-- CommonLibrary\
|--- COLORS.js\
COLORS is suppose to contain all the defines for common used colors: COLORS 假设包含常用颜色的所有定义:
export default {
COLOR_BLACK: "#000000",
COLOR_DARKBLUE: "#0F3274",
COLOR_DARKGRAY: "#999",
COLOR_LIGHTBLUE: "#6EA8DA",
COLOR_ORANGE: "#C50",
COLOR_TEXT_LIGHTBLUE: "#159EEA",
COLOR_WHITE: "#FFFFFF",
};
Regardless of what I do, I cannot import the COLORS from HomeScreen.无论我做什么,我都无法从 HomeScreen 导入颜色。 I tried:
我试过:
import COLORS from "../../CommonLibrary/COLORS"
import COLORS from "../../CommonLibrary/COLORS.js"
import {COLORS} from "../../CommonLibrary/COLORS"
import {COLORS} from "../../CommonLibrary/COLORS.js"
I tried nohoist, using index.js in the CommonLibrary folder (but I think I did that wrong).我尝试了 nohoist,在 CommonLibrary 文件夹中使用 index.js(但我认为我做错了)。 This file is just one example of what I want to do.
这个文件只是我想要做的一个例子。 I also want to make files for commonly used components like custom buttons and things, but those can be after the colors work.
我还想为常用组件(如自定义按钮和事物)制作文件,但这些文件可以在颜色起作用之后进行。
I have googled to the best of my abilities and tried what I can decipher from online but nothing has worked.我尽我所能在谷歌上搜索并尝试了我可以从网上破译的内容,但没有任何效果。
Any assistance is greatly appreciated!非常感谢任何帮助!
Inside COLORS.js it should be在 COLORS.js 里面应该是
export const colors = {
COLOR_BLACK: "#000000",
COLOR_DARKBLUE: "#0F3274",
COLOR_DARKGRAY: "#999",
COLOR_LIGHTBLUE: "#6EA8DA",
COLOR_ORANGE: "#C50",
COLOR_TEXT_LIGHTBLUE: "#159EEA",
COLOR_WHITE: "#FFFFFF",
};
Then in your HomeScreen import it like below:然后在您的 HomeScreen 中导入它,如下所示:
import {colors} from "../../CommonLibrary/COLORS"
COLORS.js should be inside project folder COLORS.js 应该在项目文件夹中
import COLORS from "../COLORS" should work从“../COLORS”导入颜色应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.