在返回 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?

查看:28
本文介绍了在返回 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.

推荐答案

是的,这是完全可行的.

Yes this is totally doable.

你应该在根或者你需要显示通知的地方有一个通知显示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.

检查这个和这个答案查看行为主体示例

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

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