跨多个项目共享角度库 [英] Share a angular library across multiple projects

查看:26
本文介绍了跨多个项目共享角度库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个由后端 + 前端组成的 Angular 7+ 项目.现在我遇到了一个要求,我需要再创建一个由前端+后端组成的项目.但是有一些可重用的前端代码可以在两个应用程序中重用.

I am working on a angular 7+ project which is composed of backend + frontend. Now i am shot with a requirement where i need to create one more project which comprises of frontend+backend. But there are some reusable frontend code that is present which could be reused across both the application.

我的项目结构

要求

所以当我阅读图书馆概念时,我可以找到

So when i read about library concepts i could find

1) 创建两个前端应用程序并共享一个公共库

我不能采用这种方法,因为我使用的是通过 Visual Studio 创建的 asp.net 角度模板项目(如图所示 此处).这两个应用程序(项目一和项目二)都有自己的 FE+BE.因此,我无法将两个 FE 应用程序组合到同一个文件夹中并让它们使用共享库.

I cannot take this approach as i am using asp.net angular template project created through the visual studio (as shown Here). The two applications (Project One and Project Two) has its own FE+BE. Hence i cannot group two FE applications inside the same folder and make them use the shared library.

2) 创建库并上传到 npm 并用作依赖项

我不能采用这种方法,因为代码是专有的,不能在网络外共享.也没有本地企业 npm 注册表来使用企业 npm.

I cannot take this approach as the code is proprietary and cannot be shared outside the network. Also there is no local corporate npm registry to use the corporate npm.

我知道这可以使用库概念来完成.但我不明白这如何跨多个应用程序完成.有人可以帮助解决这个问题.谢谢!

I know this could be done using a library concept. But i am not understanding how this could be done across multiple applications. Could someone help with a solution for this. Thanks !

推荐答案

考虑到您在 OP 中提到的目录方案,您可以通过本地路径添加您的库,如 NPM 文档中所述:本地路径:

Considering the directory scenario you mentioned in your OP, you can add your library via local path, as mentioned in the NPM docs: Local Paths:

- Root
|
 -- Project One 
|
 -- Project Two
|
 -- Library usued across both projects

例如在Project Onepackage.json中,您可以将库添加为依赖项:

For example in the package.json of Project One, you can add the library as dependency:

"dependencies" : {
  "my-shared-library" : "file:../Library usued across both projects",
}

附言我不确定库文件夹名称中是否允许使用空格,但您可以尝试找出它.但我假设您无论如何都不会使用空格.

P.S. I'm not sure if spaces are allowed in the library folder name, but you can try and find it out. But I assume you'ld not use spaces anyway.

替代解决方案和进一步计划

2) 创建库并上传到 npm 并用作依赖项我不能采用这种方法,因为代码是专有的,不能在网络外共享.也没有本地企业 npm 注册表来使用企业 npm.

2) Create the library and upload to npm and use as dependency I cannot take this approach as the code is proprietary and cannot be shared outside the network. Also there is no local corporate npm registry to use the corporate npm.

它并不完全准确.您确实可以将此方法作为替代解决方案,或者如果您认为将来可以在另一个项目中使用此库.有一种方法可以做到这一点,如 NPM 文档中所述:Git URL 作为依赖项.

It's not precisely accurate. You can indeed take this approach as an alternative solution or if you think that you could use this library in the future within another project. There is one way to do this, as mentioned in the NPM docs: Git URLs as Dependencies.

package.json

"dependencies" : {
  "my-shared-library" : "Git repo address here. There are various ways to specify an address. See below.",
}

如何添加库作为依赖

  • 假设您为此库创建了一个新存储库:
"dependencies" : {
  "my-shared-library" : "git+ssh://git@my-companys-treasure.com/my-shared-library.git",
}

这样您甚至可以使用不同版本的库.假设您为 Project One 开发了一个新版本的库(即 4.2.0),但尚未准备好或不应该被 Project 二 使用code>,在Project Onepackage.json中,你可以这样添加:

This way you can even use different versions of your library. Say you develop a new version (i.e. 4.2.0) of your library for Project One and is not ready or shouldn't be used by Project Two, in the package.json of Project One you can add it like so:

"dependencies" : {
  "my-shared-library" : "git+ssh://git@my-companys-treasure.com/my-shared-library.git#branch-v4.2.0",
}

  • 或者,您可以在当前存储库中创建一个新分支,例如 branch-for-my-shared-library(假设 Project One项目二被推送到同一个存储库):
    • Alternatively you can create a new branch, say branch-for-my-shared-library, within the current repository (Assuming both of Project One and Project Two are pushed to this same repository):
    • "dependencies" : {
        "my-shared-library" : "git+ssh://git@my-companys-treasure.com/my-current-project.git#branch-for-my-shared-library",
      }
      

      另请注意,您可以在 URL 中指定多种协议,如文档中所述:

      Also note that there are several protocols that you can specify in the URL, as mentioned in the doc:

      协议是 git、git+ssh、git+http、git+https 或 git+file 之一.

      The protocol is one of git, git+ssh, git+http, git+https, or git+file.

      进一步说明:

      快乐编码.

      这篇关于跨多个项目共享角度库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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