简体   繁体   English

如何使用angularJS在多个项目之间共享代码

[英]How to share code between multiple projects with angularJS

I was wondering what would be the best practice to share common libraries and own modules between multiple angularJS projects. 我想知道在多个angularJS项目之间共享公共库和自己的模块的最佳实践是什么。

Let's assume that I'm working on two different projects. 我们假设我正在开展两个不同的项目。 Both rely on libraries like angularJS, bootstrap etc. 两者都依赖于angularJS,bootstrap等库。

I have a file structure like below: 我有一个如下文件结构:

  • Project 1 项目1
    • index.html 的index.html
    • css CSS
    • js JS
      • module A 模块A.
      • module B 模块B.
    • lib LIB
      • angular
      • bootstrap 引导
  • Project 2 项目2
    • index.html 的index.html
    • css CSS
    • js JS
      • module B 模块B.
      • module X 模块X.
    • lib LIB
      • angular
      • bootstrap 引导

So I was thinking about just creating another directory with all the shared components so that I get sth. 所以我在考虑用所有共享组件创建另一个目录,这样我就可以了。 like: 喜欢:

  • Shared 共享
    • angular
    • bootstrap 引导
    • module B 模块B.
  • Project 1 项目1
    • index.html 的index.html
    • css CSS
    • js JS
      • module A 模块A.
  • Project 2 项目2
    • index.html 的index.html
    • css CSS
    • js JS
      • module X 模块X.

I have module B written like: 我有模块B写成:

angular.module("moduleB", [])
    .service("SB", [function () {/*functionality here*/}]);
    .factory("FB", [function () {/*functionality here*/}]);

and would then include it in my Project 1/2 as dependency like: 然后将它包含在我的Project 1/2中作为依赖项,如:

angular.module("project1", ["moduleB"]);

to achieve this approach. 实现这种方法。

Would that be the best way? 那会是最好的方式吗? What could be an alternative? 什么可以替代?

You can do it that way, but it may give you headaches if you ever want Project 1 and Project 2 to use two different versions of the Shared components. 您可以这样做,但如果您希望Project 1和Project 2使用两个不同版本的Shared组件,则可能会让您头疼。 Imagine that you need to release Project 1 with the latest shared components, but Project 2 still relies on a previous version of the shared components. 想象一下,您需要使用最新的共享组件发布Project 1,但Project 2仍然依赖于以前版本的共享组件。 What a mess. 真是一团糟。 Some other options: 其他一些选择:

  1. Make the shared components Bower components that could be included into either project as dependencies (see http://briantford.com/blog/angular-bower ) 使共享组件Bower组件可以作为依赖项包含在任一项目中(请参阅http://briantford.com/blog/angular-bower
  2. Compile all shared assets into a mutually accessible URL (see https://medium.com/@KamilLelonek/sharing-templates-between-angular-applications-b56469ec5d85 ) 将所有共享资产编译为可互访的URL(请参阅https://medium.com/@KamilLelonek/sharing-templates-between-angular-applications-b56469ec5d85
  3. Basically copy/paste components between projects, either manually or using something like gulp (see https://groups.google.com/forum/#!msg/angular/TxE5yoQkG-U/6-vWAZsqn1YJ ) 基本上在项目之间复制/粘贴组件,手动或使用gulp之类的东西(参见https://groups.google.com/forum/#!msg/angular/TxE5yoQkG-U/6-vWAZsqn1YJ

The second option has a lot of moving parts and requires changes to your applications, not to mention the inability to deploy different versions of the shared code in each project. 第二个选项有很多移动部件,需要更改应用程序,更不用说无法在每个项目中部署不同版本的共享代码。 The first has the benefit of being easy to slice into 3 repositories: Project 1, Project 2, and Shared. 第一个好处是易于切入3个存储库:项目1,项目2和共享。 If Shared is a Bower dependency in Project 1 and Project 2, a simple bower update will get you the latest version in either project. 如果Shared是项目1和项目2中的Bower依赖项,则简单的bower update将为您提供任一项目中的最新版本。 Or you can lock a project to particular versions of Shared. 或者,您可以将项目锁定到特定版本的Shared。

Option 3 seems like it gives you the most control, but you will quickly lose track of which version of Shared your project is using. 选项3似乎为您提供了最大的控制权,但您很快就会忘记您的项目使用的共享版本。 Option 1 solves that. 选项1解决了这个问题。

Another potential option is to publish your shared code as npm modules. 另一个可能的选择是将您的共享代码发布为npm模块。 If the code is private then you can use npm Private Modules for that, although there is a cost for using private modules. 如果代码是私有的,那么你可以使用npm Private Modules ,尽管使用私有模块需要付出代价。

With either this or the Bower approach described in d.jamison's answer try to break your modules into smaller modules based on their specific purposes, rather than having big monolithic "AllOurSharedCode" modules. 使用d.jamison的答案中描述的这种方法或Bower方法,尝试根据其特定目的将模块分解为更小的模块,而不是使用大型单片“AllOurSharedCode”模块。

The benefit of that is that if you find a bug, or make an improvement, you can see more easily exactly which of your projects may be affected. 这样做的好处是,如果您发现错误或进行改进,您可以更轻松地查看您的哪些项目可能会受到影响。 The require or ES2015 import syntax help a lot with that too as you will be saying eg import { calculateLineTotal } from OrderLogic.js , so it is then trivial to find all of the places in code that a change to the calculateLineTotal would impact. require或ES2015 import语法也import { calculateLineTotal } from OrderLogic.js ,例如import { calculateLineTotal } from OrderLogic.js ,因此可以import { calculateLineTotal } from OrderLogic.js找到对calculateLineTotal进行更改会影响的代码中的所有位置。

Or just do... 或者只是......

"dependencies" : {
  "shared-code" : "git://github.com/user/shared-code.git",
}

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

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