如何在项目之间共享 React 组件 [英] How to share React Components between projects
问题描述
我构建了一个要在不同项目中使用的组件.使用 NPM 跨项目重用此组件的最简单的最佳方法是什么?
I built a Component that I want to use in separate projects. What is the simplest best way to reuse this Component across projects using NPM?
推荐答案
您还没有表明是否希望将此组件保密,因此我将通过两种方式回答:
You haven't indicated whether you wish to keep this component private, so I'm going to answer two ways:
这是最简单的:将您的组件发布为 npm
包(请参阅文档 此处).然后像在您将使用组件的项目中安装任何其他程序包一样安装您的程序包.
This is easiest: publish your component(s) as an npm
package (see the docs here). Then install your package just like any other in the projects where you will be using your components.
这里有几个选项:
在
npm
上为私有模块付费(文档在这里).如果你走这条路,你可以像安装其他任何东西一样安装你的包.
pay for a private modules on
npm
(docs here). If you go this route, you can install your package just like any other.
使用 npm link
(docs)创建从项目到位于本地存储上的组件包的链接.这有效,但设置起来有点棘手.这样做的好处是,如果您重新构建组件,所有带有该组件链接的项目都会立即看到这些更改.
Use npm link
(docs) to create a link from the project(s) to the component package located on your local storage. This works but is kind of tricky to set up. The benefit of this is that if you rebuild your component, all projects with a link to the component will see those changes immediately.
使用 npm install
从您的(私人)Github 或其他存储库安装.这与普通的 npm install
类似,并且比 npm link
更容易使用.缺点是对组件的任何更改都需要推送到远程存储库,然后需要更新使用该组件的项目.
Use npm install
to install from your (private) Github or other repository. This works similarly to the normal npm install
and is a little easier to use than npm link
. The downside is that any changes to the component need to be pushed to the remote repository and then the project(s) using the component need to be updated.
这篇关于如何在项目之间共享 React 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!