Vue package webstorage storage method public call

Packaged storage.js:

var storage={
  set(key,value){
      localStorage.setItem(key,JSON.stringify(value));
  },
  get(key){
      return JSON.parse(localStorage.getItem(key));
  },
  remove(key){
      localStorage.removeItem(key);
  }
}

export default storage;

Global configuration main.js:

import storage from "./utils/storage"

Vue.prototype.$storage=storage//全局storage

Called in the component:

this.$storage.set("name",this.ruleForm.username);

 

Guess you like

Origin blog.csdn.net/SmartJunTao/article/details/108461937