简体   繁体   English

是否可以使用rxjs angular 5在其他选项卡浏览器中订阅observable

[英]is it possible to subscribe observable in other tab browser with rxjs angular 5

I have an angular app, and there are two pages and I open them in each tab of browser, if I do some method in page 1, I want page 2 subscribe data change with rxjs and update data in page 2 like realtime app, is there any way to do that without websocket?, I've tried it with subscribe method but only page 1 listen that subscribe. 我有一个角度应用程序,有两个页面,我在浏览器的每个选项卡中打开它们,如果我在第1页中执行某些方法,我希望第2页订阅数据更改为rxjs并更新第2页中的数据,如实时应用程序,是有什么办法可以在没有websocket的情况下做到这一点吗?我用subscribe方法尝试了它,但只有第1页听了订阅。 this is page 1 called refresh method to update boolean value 这是第1页称为刷新方法来更新布尔值

this.service.doRefresh();

page 2 listen 第2页听

this.service.$refresh.subscribe(x=>{//boolean value changed by page 1(tab1)
    if(x) {
        console.log(x);
        this.service.stopRefresh();
    }
});

You can actually add event listeners to localStorage so you can use localStorage to "pass messages" between tabs 您实际上可以将事件侦听器添加到localStorage,以便您可以使用localStorage在选项卡之间“传递消息”

localStorage.setItem('yourKeyName', true)

window.addEventListener('storage', (event) => {
   if (event.key == 'yourKeyName'){
      //update data
   }
});

So when you change the value of something in localStorage on one page the other page can subscribe to the event of it changing 因此,当您在一个页面上更改localStorage中某些内容的值时,另一个页面可以订阅它更改的事件

Browser support and more info: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#Browser_compatibility 浏览器支持和更多信息: https//developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#Browser_compatibility

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM