[英]Import path differences in React and React Native
I am trying to share logic components between a React appliacation and a React Native application with a similar structure as shown below: 我试图在React appliacation和React Native应用程序之间共享逻辑组件,其结构如下所示:
-source
|-appLogic
|-logicComponent1.js
|-logicComponent2.js
|-moreAppLogic
|-moreLogic.js
|-appviews
|-reactView.js
|-reactNativeView.js
I have a problem during the import of modules in the logic components. 我在导入逻辑组件中的模块时遇到问题。 The reactView.js will import the logic components like this: reactView.js将导入逻辑组件,如下所示:
import {moreLogic} from "appLogic/moreAppLogic/moreLogic.js"
whereas the reactNativeView.js will import the same file like this: 而reactNativeView.js将导入相同的文件,如下所示:
import {moreLogic} from "../appLogic/moreAppLogic/moreLogic.js"
My problem arises in the case of moreLogic.js importing for example logicComponent1. 我的问题出现在moreLogic.js导入例如logicComponent1的情况下。 Since react, in the moreLogic.js, will want something like this: 因为在moreLogic.js中,react会想要这样的东西:
import {logicComponent1} from "appLogic/logicComponent1.js"
and react-native will want: 和本地反应将需要:
import {logicComponent1} from "../logicComponent1.js"
I have tried to do the import in moreLogic.js both ways but if I do it the react way, the native app cant find the file and vice versa.. 我试图在moreLogic.js中进行导入两种方式,但如果我这样做反应方式,本机应用程序无法找到该文件,反之亦然。
Is there a way to go around this problem?? 有办法解决这个问题吗? I cannot change the file structure, that is really not an option. 我无法更改文件结构,这实际上不是一个选项。
put package.json
file in source/appLogic
with the following content: 将package.json
文件放在source/appLogic
其中包含以下内容:
{
"name": "appLogic"
}
After that you can use import {moreLogic} from "appLogic/moreAppLogic/moreLogic.js"
in both react js and react native 之后,您可以在两个react js中使用import {moreLogic} from "appLogic/moreAppLogic/moreLogic.js"
并做出本机反应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.