简体   繁体   English

如何重用项目不同文件夹中的角度组件?

[英]How to reuse angular components from different folders of the project?

I have an Angular project where the previous developers have copy / pasted components. 我有一个Angular项目,以前的开发人员在其中复制/粘贴了组件。

In this project mobile view is completely different from the desktop view, except some components and those components which could be reused, they are exactly copy/pasted by the developers. 在此项目中,移动视图与桌面视图完全不同,除了某些组件和可以重用的那些组件外,它们完全由开发人员复制/粘贴。 And even the '.ts' files of the different template views are also copy/pasted which could be reused. 甚至不同模板视图的“ .ts”文件也被复制/粘贴,可以重复使用。

Now I have two question 现在我有两个问题

  1. If I make a shared module and reuse that module in both the desktop and mobile folder does it have any impact on the speed / performance of the application as there are more than 50 components which needs to be done ? 如果我制作一个共享模块并在桌面文件夹和移动文件夹中重用该模块,那么对应用程序的速度/性能是否有任何影响,因为需要完成50多个组件?
  2. Is it a standard to copy/paste code rather to reuse those components.? 复制/粘贴代码而不是重用那些组件是一种标准吗?

You can get a clear picture of my question by seeing the attached image link below. 通过查看下面的图片链接,您可以清楚地了解我的问题。 https://ibb.co/DgT4rwh https://ibb.co/DgT4rwh

It is not ok to copy/paste components, you should use shared modules or libraries for that, see docs for the reference: 复制/粘贴组件是不正确的,您应该为此使用共享模块或库,请参阅文档以获取参考:

https://next.angular.io/guide/file-structure https://next.angular.io/guide/file-structure

https://next.angular.io/guide/libraries https://next.angular.io/guide/libraries

https://next.angular.io/guide/styleguide https://next.angular.io/guide/styleguide

We should follow DRY principle of coding. 我们应该遵循DRY编码原则。 You should create a shared module containing the duplicate components , which will be used by other modules. 您应该创建一个包含重复组件的共享模块,其他模块将使用它们。

Vitalii Bobrov's answer is the correct way to do it when re-using modules and components across projects. Vitalii Bobrov的答案是在项目中重复使用模块和组件时的正确方法。 But from that screenshot it looks like they might be copying/pasting inside the same project. 但是从该屏幕截图看来,他们可能正在同一项目中进行复制/粘贴。 You should always consolidate and reuse modules/components/services. 您应该始终合并和重用模块/组件/服务。 The only impact it will have on speed will be to lower your application's overall size (fewer components = less code) and increase initial load time as a result. 它对速度的唯一影响是减小应用程序的整体大小(更少的组件=更少的代码)并因此增加初始加载时间。 And as for #2, it is never standard to copy/paste code and that is a hallmark of an inexperienced developer in my experience. 至于#2,复制/粘贴代码从来都不是标准的,这是我经验不足的开发人员的标志。

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

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