import { save , get } from './save'
import { CART_KEY } from './key'
export default class Local{
//暴露給外邊的localstorage存儲(chǔ)接口
static saveCartData(value){
save(CART_KEY,value)
}
static getCartData(){
get(CART_KEY)
}
}
key.js
export const CART_KEY = 'cartkey'
save.js
export function save(key,value){
if(typeof(value)==='object'){
value = JSON.stringify(value)
}
localStorage.setItem(key,value)
}
//獲取localstorage的接口
export function get(key){
return localStorage.getItem(key)
}
Home.vue
//存儲(chǔ)
//導(dǎo)入存儲(chǔ)模塊
import Local from './index.js'
Local.saveCartData(存儲(chǔ)的數(shù)據(jù))
//獲取
//導(dǎo)入獲取模塊
import { get } from './save.js'
//轉(zhuǎn)json數(shù)據(jù)
JSON.parse(get('cartkey'))