vue工具类之——Cookie操作

注意:此工具是个鸡肋的存在,没有多大意义。(浏览器可以直接操作,类似于LocalStorage)


/**

* description: CooKie

* create_time: 2019-01-03

* message: 不接受任何私下吐槽,请当面沟通协调

*/

class Cookie {

  constructor() {

    this.date= new Date();

  }

  // 存入COOKIE

  set (name, value, day) {

    day= day|| 7;

    const valueType = typeof value;

    value= (valueType == 'string')? JSON.stringify(value):value;

    this.date.setTime(this.date.getTime() + 24 * 60 * 60 * 1000 * day);

    window.document.cookie= name+ '=' + value+ ';path=/;expires=' + this.date.toGMTString();

  }

  // 取出COOKIE

  get (name) {

    let v = window.document.cookie.match('(^|;) ?' + name+ '=([^;]*)(;|$)');

    return v ? v[2] : null

  }

  // 删除COOKIE

  del (name) {

    this.date.setTime(this.date.getTime() - 1)

    let val = this.get(name)

    if (val != null) {

      document.cookie= name+ '=' + val + ';expires=' + this.date.toGMTString();

    }

}

}

export default Cookie


用法:
const Cookie = new cookie();
Cookie.set('A','TEST_TOKEN_Ra')



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容