index.js
import { save , get } from './save'
import { CART_KEY } from './key'
export default class Local{
//暴露给外边的localstorage存储接口
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
//存储
//导入存储模块
import Local from './index.js'
Local.saveCartData(存储的数据)
//获取
//导入获取模块
import { get } from './save.js'
//转json数据
JSON.parse(get('cartkey'))