封装 - 本地存储

1、封装

function storage(key, value = undefined) {
    if (value !== undefined) {
        value = typeof value == 'string' ? value : JSON.stringify(value);
        console.log(value,'value******values')
        localStorage[key] = value;
        return;
    }
    try {
        value = localStorage[key];
        if (value && value.indexOf('{"') >= 0)
            return JSON.parse(value);
        return value || '';
    } catch (err) {
        return localStorage[key];
    }
};

2、使用

(1)存入本地存储

storage('account', item.value);

(2)从本地存储取出

storage('account')
发布了44 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/LiaoFengJi/article/details/98589815