美文网首页
vue 本地存储15天

vue 本地存储15天

作者: 吃肉肉不吃肉肉 | 来源:发表于2021-07-06 15:13 被阅读0次
created() {
    if (this.getCookie('dataPasswordAdmin')) {
      // 存在
    } else {
      // 不存在
    }
  },
methods: {
  //提交按钮
  checkDataPassword() {
      this.dataPassword = 123
      // 密码存储日期 15天
      this.setCookie('dataPasswordAdmin', this.dataPassword, 'd15')
  },
 // 存储cookie
 setCookie(name, value, time) {
      var strsec = this.getsec(time)
      var exp = new Date()
      exp.setTime(exp.getTime() + strsec * 1)
      document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
  },
 //转换时间
  getsec(str) {
      var str1 = str.substring(1, str.length) * 1
      var str2 = str.substring(0, 1)
      if (str2 === 's') {
        return str1 * 1000
      } else if (str2 === 'h') {
        return str1 * 60 * 60 * 1000
      } else if (str2 === 'd') {
        return str1 * 24 * 60 * 60 * 1000
      }
  },
  // 判断cookie存储的数据是否存在
  getCookie(name) {
      var arr = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      if (document.cookie.match(reg)) {
        arr = document.cookie.match(reg)
        return unescape(arr[2])
      } else {
        return null
      }
  },
}

相关文章

网友评论

      本文标题:vue 本地存储15天

      本文链接:https://www.haomeiwen.com/subject/jpegultx.html