【问题标题】:Clear storage with addEventListener使用 addEventListener 清除存储
【发布时间】:2021-09-12 20:29:05
【问题描述】:

我正在尝试使用按钮和 addEventListener 清除本地存储。但它不起作用,我不知道为什么。谢谢。

    const clearStorage = document.querySelector(".clear-button");

    clearStorage.addEventListener("click", (function(){
        localStorage.clear();
    }));
};

这段代码被导入到下面的脚本中:

import { getFavourites } from "./utils/getFavs.js";
import createMenu from "./components/createMenu.js";
import displayMessage from "./components/displayMessage.js";
import { clearFavList } from "./components/clearFavList.js"

createMenu();
getFavourites();

const favouriteList = getFavourites();
const articlesContainer = document.querySelector(".favourites-container");
if(!favouriteList.length) {
    displayMessage("error", "You don't have any saved favourites yet.", ".favourites-container");
}
favouriteList.forEach((favourite) => {
    articlesContainer.innerHTML += `<div class="article">
                                    <div class="article-content-text">
                                        <h2 class="article-title fav-wrapper-text">Title: ${favourite.title}</h2>
                                    </div>
                                    <div>
                                        <i class="fas fa-heart favButton"></i>
                                    </div>
                                    </div>`;
});

clearFavList(favouriteList);

【问题讨论】:

标签: javascript dom-events web-storage


【解决方案1】:

此代码来自 React 身份验证组件,具有处理存储的所有基本功能。

// you can create multiple storage stores
const LOCAL_STORAGE_STORE = 'storage_sample';

export const getHasLocalStorageAuth = () => {
    // check local storage
    const localStorage = __getLocalStorage(LOCAL_STORAGE_STORE);
    return { status: !!localStorage, data: localStorage.auth };
};

export const clearLocalStorageAuth = () => {
    __clearLocalStorage(LOCAL_STORAGE_STORE);
    return;
};

export const setLocalStorageAuth = (newLocalStorage: any) => {
    __setLocalStorage(LOCAL_STORAGE_STORE, newLocalStorage);
    return;
};

// setting data to localstorage
export function __setLocalStorage(
    localStorageName: string,
    localStorageValue: any,
    isJson = true
) {
    if (isJson) {
        localStorage.setItem(localStorageName, JSON.stringify(localStorageValue));
    } else {
        localStorage.setItem(localStorageName, localStorageValue);
    }
}

// getting data from localstorage
export function __getLocalStorage(localStorageName: string): any {
    let localStorageValue: any;
    if (localStorage.getItem(localStorageName) !== null) {
        localStorageValue = localStorage.getItem(localStorageName);
    } else {
        localStorageValue = false;
    }

    return JSON.parse(localStorageValue);
}

// clear data from localstorage
export function __clearLocalStorage(localStorageName: string | null) {
    localStorage.clear();
}
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
相关资源
最近更新 更多