VUE 使用 localStorage

客户端存储 — Vue.js
https://cn.vuejs.org/v2/cookbook/client-side-storage.html

支持更好用的 API 的库

RobinCK/vue-ls: Vue plugin for work with local storage, session storage and memory storage from Vue context
https://github.com/RobinCK/vue-ls

main.js

const optionsLS = {
    
    
  namespace: "ls__",
  name: "ls",
  storage: "local",
};

const optionsSS = {
    
    
  namespace: "ss__",
  name: "ss",
  storage: "session",
};

Vue.use(Storage, optionsLS);
Vue.use(Object.assign({
    
    }, Storage), optionsSS);

示例

  mounted() {
    
    
    this.email = this.$ls.get("email");
    this.email = this.$ss.get("email");
  },
  methods: {
    
    
      xxx() {
    
    
          this.$ls.set("email", this.email);
          this.$ss.set("email", this.email);
      }
  }

Guess you like

Origin blog.csdn.net/MAIMIHO/article/details/121152620