简体   繁体   English

如何从共享组件文件夹导入不同的应用程序? 反应 / 反应原生

[英]How can different apps import from a shared components folder? react / react-native

I'm currently working on a project where we have to make 2 mobile apps.我目前正在做一个项目,我们必须制作 2 个移动应用程序。 We decided to build those apps with react-native, so we can take advantage of the cross platform functionality.我们决定使用 react-native 构建这些应用程序,以便我们可以利用跨平台功能。

Then we outlined the project structure.然后我们概述了项目结构。 In the UI design of both apps, most of the components are exactly the same, but only a change in color.两款APP的UI设计中,大部分组件都一模一样,只是颜色有所变化。

So that's why we've decided that both apps must import (flexible) components from one folder .所以这就是为什么我们决定两个应用程序都必须从一个文件夹导入(灵活)组件。

Project Structure项目结构

- mobile
    - project 1
    - project 2

Must look something like one off the two solutions below必须看起来像以下两种解决方案之一

- components
- mobile
    - project 1
    - project 2

or或者

- mobile
    - components
    - project 1
    - project 2

' '

The problem问题

So the problem occurs when I try to import the components from outside either the project 1 or 2. For example, I created a Loading component usable for both projects (apps).因此,当我尝试从项目 1 或 2 外部导入组件时会出现问题。例如,我创建了一个可用于两个项目(应用程序)的加载组件。 But this component uses a third-party library, and that library requires a Pod install, to install some additional libraries.但是这个组件使用了一个第三方库,这个库需要安装 Pod 来安装一些额外的库。

Questions:问题:

  1. Where should I install the third party library (yarn add).我应该在哪里安装第三方库(纱线添加)。 In the folder components, or in both project folders?在文件夹组件中,还是在两个项目文件夹中?
  2. If I install it in the components folder, I am not able to run Pod install, since there was nothing added inside that project directory.如果我将它安装在 components 文件夹中,我将无法运行 Pod install,因为该项目目录中没有添加任何内容。
  3. If I try to import a component from the components folder, which is outside the project directory, I'am getting 'Unable to resolve module..' error.如果我尝试从项目目录外的 components 文件夹中导入组件,则会收到“无法解析模块..”错误。

I think that react-native is not encouraging that we should import components from outside the project directory, hence the error.我认为 react-native 并不鼓励我们应该从项目目录之外导入组件,因此会出现错误。 But still, I want this to work, so it would be better organised and way more efficient.但是,我仍然希望它起作用,因此它会更好地组织并且更有效率。

Does someone have experience in this type of field?有人在此类领域有经验吗? How can a I solve this issue?我该如何解决这个问题? What are the possible solution(s) .可能的解决方案是什么

I've read other StackOverflow posts, but still couldn't figure it out.我已经阅读了其他 StackOverflow 帖子,但仍然无法弄清楚。 It is still not clear to me.我还不清楚。

If you want to share the components between two discrete projects, the common solution is to create your own NPM library as jmargolisvt suggests in their comment.如果您想在两个独立项目之间共享组件,常见的解决方案是创建您自己的 NPM 库,如 jmargolisvt 在他们的评论中所建议的那样。

It will add a bit of overhead to publishing changes to your component library though.但是,它会增加一些开销来将更改发布到您的组件库。 If you need to move fast, you can install you component library into your project by installing directly from Github for example.如果你需要快速移动,你可以通过直接从 Github 安装来将你的组件库安装到你的项目中。 See NPM's docs for the npm install syntax for this: https://docs.npmjs.com/cli/install有关npm install语法,请参阅 NPM 的文档: https : //docs.npmjs.com/cli/install

Either way, your structure ends up look like the below, and you'll be able to import your shared components as you would any other npm module.无论哪种方式,您的结构最终都如下所示,您将能够像导入任何其他 npm 模块一样导入共享组件。

app-ios
  - depends on app-component-library
app-android
  - depends on app-component-library
app-component-library

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM