观察js中的localstorage变化

问题描述 投票:0回答:2

我有一个单页应用程序,我需要对localstorage中的每个更改做出反应,它看起来像:

    MyComponent {

    someFuncWhichIsCalledEveryTimeWhenLocalStorageChanges() {
        console.log('local storage changed!);
    }

    funcThatChangesLocalStorage() {
        localstorage.setItem('key',val);
        localstorage.getItem('key')
    }

    }

我试图使用localstorage事件:

window.addEventListener('storage', function(event){
       ...
});

但那没有用。所以我正在考虑使用qazxsw poi,只是不知道如何正确实施。

javascript reactjs typescript local-storage
2个回答
2
投票

Observable<> window.addEventListener('storage', ...)

确保使用正确的works

event properties


0
投票

在另一个文档的上下文中修改存储区域(localStorage或sessionStorage)时,将触发存储事件。

Here is a basic example of the event listener.

© www.soinside.com 2019 - 2024. All rights reserved.