如何让 Angular 组件和服务更加 DRY?

发布于 2025-01-12 21:35:04 字数 494 浏览 2 评论 0原文

我发现自己在所有 Angular 应用程序中都遇到了同样的问题,而且我很难知道如何通过 Google 来寻求解决方案。

问题:我觉得我编写了很多组件和服务,它们做的事情非常相似,但我不太清楚如何使它们更加通用。在我的示例应用程序中,我有候选人和员工组件。每个都有相应的服务。

https://stackblitz.com/edit/ angular-ivy-92xryd?file=src/app/app.component.html

我无法抗拒这样的感觉:我可以编写类似于 HOF 的东西,可以帮助我“生成”这些组件和服务在飞行中。编写这些基本上执行相同 CRUD 类型操作的组件和服务感觉非常多余。而且它似乎抑制了可扩展性。我不确定我到底在寻找什么,但似乎我错过了一些东西。感谢您的帮助!

I find myself running into the same problem in all of my Angular applications and I'm having the hardest time knowing what to Google for a solution.

The problem: I feel like I write a lot of components and services that do VERY similar things but I can't quite figure out how to make them more generic. In my example app, I have candidates and employees components. Each has a corresponding service.

https://stackblitz.com/edit/angular-ivy-92xryd?file=src/app/app.component.html

I can't fight the feeling that I could write something similar to a HOF that could help me sort of "generate" these components and services on the fly. It just feels very redundant to be writing these components and services that are basically performing the same CRUD-type operations. And it seems like it inhibits scalability. I'm not sure exactly what it is I'm looking for but it seems like I'm missing something. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2025-01-19 21:35:04

看来候选人和员工有一个共同点,那就是“做人”。您可以提取共同的“人”组件来显示他们俩的详细信息。您还可以在某些 app-person 组件中使用 来添加额外的内容,即特定于员工或候选人,在这种情况下,主机组件指定应添加到每个员工或候选人的内容。

Looks like candidate and employee shares common factor, which is a "beeing a person" . You can extract common "person" component to display details of both of them. Also you could use <ng-content> or <ng-template> in some app-person component to add extra content that is specific to employee or candidate in which case HOST component specifies what should be added to each employee or candidate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文