返回API的响应并且用户导航到其他页面后,是否可以向用户显示通知? [英] Is it possible to show a notification to the user after the response from an API is returned and the user has navigated to a different page?

查看:63
本文介绍了返回API的响应并且用户导航到其他页面后,是否可以向用户显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于缺少代码的情况,我深表歉意,我只是想知道是否可以做到,以及允许我使用哪些API/库.

Firstly, apologies for the lack of code on this, I'm just looking for clarity on if it can be done and what APIs/libraries will allow me to do so.

我有一个用Angular编写的Web应用程序前端.由于服务器的位置和所返回数据的性质,它从中获取数据的后端服务之一通常可能需要2-3分钟才能返回.现在,我在前端显示了一个动画微调框,直到返回响应为止,这并不好,因为这会阻止用户在收到响应之前做任何事情.我想知道的是,是否可以从前端发送请求并允许用户在站点中导航,直到返回响应?

I have a web app front-end written in Angular. One of the back-end services it fetches data from can often take 2-3 mins to return, due to the location of the server and the nature of the data returned. Right now I have an animated spinner displayed on the front-end until the response is returned, which isn't great, as this prevents the user from doing anything until it receives a response. What I'd like to know, is it possible to send a request from the front-end and allow the user to navigate around the site until a response is returned?

类似的东西:

单击按钮>已发送API调用>用户导航到另一个页面>用户导航另一个页面>返回API响应>前端短暂显示通知,以让用户知道其操作已完成.

Click button > API call sent > user navigates to another page > user navigates another page > API response is returned > notification briefly displayed in the front-end to let the user know their action has complete.

我一直在研究推送通知,但希望通知是实际的HTML Web元素.

I was looking into push notifications but would prefer the notification to be an actual HTML web element.

推荐答案

是的,这完全可行.

您应该在需要显示通知的根或位置上有一个通知显示component.现在,该组件应该从通知service中获取数据,您可以在完成上述API之后存储响应.

You should have a notification display component at the root or the place where you need to display the notifications. Now this component should get the data from a notifications service where you can store the response after your said API is complete.

您需要做的就是在NotificationService中使用Subject/BehaviourSubject,并在API调用完成后将新值/结果推送给它.现在,通知组件将获得新的值/响应并根据需要显示消息.

All you need to do is to use a Subject/BehaviourSubject in NotificationService and push the new value/result to it after API call is finished. Now the notification component will get the new value/response and display the message as needed.

选中此选项并此答案以查看

Check this and this answer to see BehaviourSubject example

这篇关于返回API的响应并且用户导航到其他页面后,是否可以向用户显示通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆