使用NGXS,在调用本地状态之前,建议打电话给后端的推荐方法是什么
我正在启动一个新的角度应用程序并为团队进行设置。 我想知道一件事:您将访问后端的逻辑放在哪里?
在我的(第一个)案例中,关于帐户/身份验证,我需要存储用户名/电子邮件/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 技术交流群。

我认为带有 Facade 设计模式的 NGXS 在这种情况下很有帮助,它可以:
您可以在此处阅读有关 NGXS Facade 的更多信息:
https://medium.com/ngxs/ngxs-facade -3aa90c41497b
I think the NGXS with Facade design pattern is helpful in this case, which can:
You can read more about NGXS Facade here:
https://medium.com/ngxs/ngxs-facade-3aa90c41497b