简体   繁体   English

Angular 14 - 什么方法是最佳编码实践 angular 组件

[英]Angular 14 - what method would be best coding practice angular components

I want to take various components from other apps I built (2-3 from one, 3-5 from other) in various tutorials and "import"/use them into an app I am building.我想在各种教程中从我构建的其他应用程序中获取各种组件(一个中的 2-3 个,其他的 3-5 个),并将它们“导入”/使用到我正在构建的应用程序中。 (not a multi project workspace). (不是多项目工作区)。

While I've seen many topics on this, none seem to answer my intermediate coding pallett, except this: Angular 2: How to include a component from one running Angular project to another Angular project?虽然我已经看到很多关于这方面的主题,但似乎没有人回答我的中间编码托盘,除了这个: Angular 2:如何将一个正在运行的 Angular 项目中的组件包含到另一个 ZC31C335EF37283C451B18BA0DD317DE1 项目? . . The answer is vague.答案很模糊。

What would the coding best practice be?编码最佳实践是什么?

Code-CLI代码-CLI

Main app file structure (initial setup), highlighted components I wish to use:主要应用程序文件结构(初始设置),我希望使用的突出显示的组件:

我希望使用的突出显示的组件

Note: all done is VSCode & @angular/cli注意:全部完成是 VSCode 和 @angular/cli

Method 1方法一

D:/app-root/app]$ ng g c [component-from-2nd-app

I would do this for each component.我会为每个组件执行此操作。 After that, go back and copy/paste each file...之后,go 返回并复制/粘贴每个文件...

This method seems wrong or tedious, or am I missing something?这种方法似乎错误或乏味,或者我错过了什么?

Method 2方法二

Instead of generating a new component, just copy and paste the components, then go into app.module, app.component, app-routing and up those pages to reflect the changes.无需生成新组件,只需复制并粘贴组件,然后将 go 复制并粘贴到app.module, app.component, app-routing和这些页面中以反映更改。

Alternate method?替代方法?

I'm still learning angular and not totally familiar with 'libraries' - could/should/how would I "collect" components, put them in a library for use in any application I may do or currently am building?我仍在学习 angular 并且不完全熟悉“库” - 我可以/应该/如何“收集”组件,将它们放入库中以用于我可能做或目前正在构建的任何应用程序?

Summarized questions总结问题

Which method is best coding practice to use/recreate/import components from another source?哪种方法是从其他来源使用/重新创建/导入组件的最佳编码实践? And what would the syntax look like?语法会是什么样子?

A good practice is to use "Method one"一个好的做法是使用“方法一”

Once a component is created in your application, you need to migrate the code part only在您的应用程序中创建组件后,您只需迁移代码部分

Why?为什么?

You don't even care about imports of components created, Agular CLI will do for you.你甚至不需要关心创建的组件的导入,Agular CLI 会为你做。 And you just need to focus on the reference project and move logic into the newly created component你只需要专注于参考项目并将逻辑移动到新创建的组件中

This way you easily create components for the new project这样您就可以轻松地为新项目创建组件

Note: Might be you needed to re-write codes because say for example you want to import a component from angular2 to angular 14 then some code syntax might not be worked注意:可能您需要重新编写代码,因为例如您想将组件从 angular2 导入 angular 14 那么某些代码语法可能无法使用

I hope you are clear with my suggestion我希望你清楚我的建议

In a simple case where you do not want (can not) to publish.在您不想(不能)发布的简单情况下。 Simplest thing you can do is create a component library project in a angular workspace.您可以做的最简单的事情是在 angular 工作区中创建一个组件库项目。 you can use this library project components within the workspace without publishing.您可以在工作区中使用此库项目组件而无需发布。

For this to work I guess your other projects should be within same workspace.为此,我想您的其他项目应该在同一个工作区中。

Read: https://angular.io/guide/creating-libraries阅读: https://angular.io/guide/creating-libraries

Is this change is not possible for current projects.对于当前项目,是否无法进行此更改。 For now in your case it is better to go with method 1.现在,在您的情况下,最好使用方法 1 go。

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

相关问题 angular 中订阅方法的最佳实践是什么? - What is the best practice for subscribe method in angular? 对于大多数相似但位置不同的组件,角度最佳实践是什么? - What's the Angular best practice for components that are mostly similar but differ in places? 关于 Angular 中的 routerLink 和展示组件的最佳实践是什么? - What is the best practice regarding routerLink and presentational components in Angular? Angular中根据条件显示组件的最佳实践 - Best practice to display components based on conditions in Angular 在多个环境中使用的angular4应用中对URL进行编码的最佳实践是什么? - What is best practice for coding urls in an angular4 app used in multiple environments? 在 Angular 组件之间通过服务共享地图/对象/数组中的数据的最佳实践是什么 - What is the best practice to share data in map/object/array by service among Angular components 向服务的可观察性订购组件时的最佳实践是什么? [Angular 2+] - What is the best practice when subscribing components to a service's observable? [Angular 2+] Angular 创建泛型方法的最佳实践 - Angular Best Practice on creating Generic method 在 Angular 8 中监听 DOM 事件的最佳实践是什么? - What is the best practice to listening to DOM event in Angular 8? 以角度返回错误时的最佳做法是什么 - What is the best practice when returning errors in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM