同源页面监听缓存改变页面

北极熊的菜 / 2024-05-01 / 原文

onActivated(() => {
            this.searchRecordList.value = localStorage.getItem('searchRecord') ? JSON.parse(localStorage.getItem('searchRecord') as string) : [];this.getList();
            window.addEventListener('storage', this.getItem);
        });
        onDeactivated(() => {
            window.removeEventListener('storage', this.getItem);
        });
 protected getItem = () => {
        this.searchRecordList.value = localStorage.getItem('searchRecord') ? JSON.parse(localStorage.getItem('searchRecord') as string) : [];
    };