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?

旧时模样 提交于 2019-12-13 05:30:17

问题


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.

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?

Something like:

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.

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


回答1:


Yes this is totally doable.

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.

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



来源:https://stackoverflow.com/questions/55423325/is-it-possible-to-show-a-notification-to-the-user-after-the-response-from-an-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!