Vue は一般的な localStorage-tool メソッドを設定します

localStorage は Web サイト全体のデータを長期保存するために使用され、保存されたデータは手動で削除されるまで有効期限がありません。

ヒント:

現在のセッションのデータのみを保存したい場合は、 sessionStorage属性を使用できます。このデータ オブジェクトは、同じウィンドウ (またはタブ) のデータを一時的に保存し、ウィンドウまたはタブを閉じるとデータは削除されます。

utils.js ファイル:

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
export const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
export const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}

使用法:

/**导入localStorage*/
import{setStore,getStore} from @/config/utils
setStore('userName',xxxxx)/**存储用户名值为xxxx*/
getStore('userName')/**获取用户名称*/

おすすめ

転載: blog.csdn.net/pinhmin/article/details/129160854