로컬 스토리지 모니터링 - 실시간 데이터 업데이트

1665 단어 jsvue-js


Vue.prototype.$setItem = function(key, newVal) {
                if (key === 'n_title') {
                    //  StorageEvent 
                    let newStorageEvent = document.createEvent('StorageEvent');
                    const storage = {
                        setItem: function(k, val) {
                            localStorage.setItem(k, val);
                            //  
                            newStorageEvent.initStorageEvent('setNotice', false, false, k, null, val, null, null);

                            //  
                            window.dispatchEvent(newStorageEvent)
                        }
                    }
                    return storage.setItem(key, newVal);
                }
                if (key === 'n_content') {
                    //  StorageEvent 
                    let newStorageEvent = document.createEvent('StorageEvent');
                    const storage = {
                        setItem: function(k, val) {
                            localStorage.setItem(k, val);
                            //  
                            newStorageEvent.initStorageEvent('setNotice', false, false, k, null, val, null, null);
                            //  
                            window.dispatchEvent(newStorageEvent)
                        }
                    }
                    return storage.setItem(key, newVal);
                }
 }


 
this.$setItem('n_content', ‘content');
this.$setItem('n_title', 'title');


 
window.addEventListener('setNotice', () => {
      ... 
 })

좋은 웹페이지 즐겨찾기