使用NGXS,在调用本地状态之前,建议打电话给后端的推荐方法是什么

发布于 01-19 15:39 字数 402 浏览 1 评论 0原文

我正在启动一个新的角度应用程序并为团队进行设置。 我想知道一件事:您将访问后端的逻辑放在哪里?

在我的(第一个)案例中,关于帐户/身份验证,我需要存储用户名/电子邮件/accessToken,并且通常每次收到令牌时,我想启动一个计时器以在访问令牌过期之前刷新它。

经过一些研究,我发现有些人建议在我所在州的操作处理程序中进行 API 调用。但老实说,这感觉有点像混合不同的职责,而且由于这将是一个相当大的应用程序,我有点担心我们最终会得到一个巨大的状态类。同样在这种情况下,当我收到令牌时,我需要一个计时器来刷新它。我更倾向于将 NGXS 视为一个库来对我的状态应用突变并传播更改,而不是应该负责具有这种逻辑的东西(我可能是错的?)

那么,是否有推荐的方法使用 NGXS 来做到这一点?我们是否应该调用一个由 API 调用的服务,然后调度一个操作?

I'm starting a new angular app and doing the setup for the team.
One thing I'm wondering: where do you put the logic to access the backend?

In my (first) case, about the accounts/auth, I need to store, username/email/accessToken, and typically every time a token is received, I want to start a timer to refresh the access token before it expires.

After some research, I see that some people recommend having the API call in the action handler in my State. But honestly, it feels a bit like mixing different responsibility, and since this will be for quite a big application, I'm a bit concerned that we end up with a state class that will be huge. Also in this case, when I receive my token, I need to have a timer to refresh it. I was more seing NGXS as a library to apply mutation on my state and propagate changes, not something should be responsible of having such logic(I'm maybe wrong?)

So, is there a recommended way of doing this with NGXS? Should we call a service that does the API call and then dispatch an action?

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

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

发布评论

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

评论(1

萝莉病2025-01-26 15:39:00

我认为带有 Facade 设计模式的 NGXS 在这种情况下很有帮助,它可以:

  • 通过屏蔽提高软件库的可读性和可用性
    与单个(通常是
    简化)API;
  • 为更通用的内容提供特定于上下文的接口
    功能(完成特定于上下文的输入验证);
  • 发球
    作为更广泛重构整体或
    紧密耦合的系统有利于更松散耦合的代码。

您可以在此处阅读有关 NGXS Facade 的更多信息:

https://medium.com/ngxs/ngxs-facade -3aa90c41497b

I think the NGXS with Facade design pattern is helpful in this case, which can:

  • Improve the readability and usability of a software library by masking
    interaction with more complex components behind a single (and often
    simplified) API;
  • Provide a context-specific interface to more generic
    functionality (complete with context-specific input validation);
  • Serve
    as a launching point for a broader refactor of monolithic or
    tightly-coupled systems in favor of more loosely-coupled code.

You can read more about NGXS Facade here:

https://medium.com/ngxs/ngxs-facade-3aa90c41497b

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