我有一个单页应用程序,我需要对本地存储中的每个更改做出反应,它看起来像:
MyComponent {
someFuncWhichIsCalledEveryTimeWhenLocalStorageChanges() {
console.log('local storage changed!);
}
funcThatChangesLocalStorage() {
localstorage.setItem('key',val);
localstorage.getItem('key')
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 localstorage 事件:
window.addEventListener('storage', function(event){
...
});
Run Code Online (Sandbox Code Playgroud)
但这不起作用......所以我正在考虑使用Observable<>,只是不知道如何正确实现它。