美文网首页
添加vue-cookie

添加vue-cookie

作者: 君临12138 | 来源:发表于2018-11-26 15:59 被阅读0次

    1 安装vue-cookie

    cnpm install --save vue-cookie
    

    2 引入

    在main.js中进行引入:

    import Vue from 'vue' //这句是原来就有的
    import VueCookie from 'vue-cookie'
    Vue.use(VueCookie)
    

    安装引入后就可以使用了

    3 使用方法

    // From some method in one of your Vue components
    this.$cookie.set('test', 'Hello world!', 1);
    // This will set a cookie with the name 'test' and the value 'Hello world!' that expires in one day// To get the value of a cookie use
    this.$cookie.get('test');// To delete a cookie usethis.$cookie.delete('test');
    

    3.2 高级例子

    // Setting the cookie Domain
    this.$cookie.set('test', 'Random value', {expires: 1, domain: 'localhost'});
    // As this cookie is set with a domain then if you wish to delete it you have to provide the domain when calling delete
    this.$cookie.delete('test', {domain: 'localhost'});
    // Customizing expiresvar date = new Date;
    date.setDate(date.getDate() + 21);
    this.$cookie.set('dateObject', 'A date object', { expires: date });
    this.$cookie.set('dateString', 'A parsable date string', { expires: date.toGMTString() })
    ;this.$cookie.set('integer', 'Seven days later', { expires: 7 });
    this.$cookie.set('stringSuffixY', 'One year later', { expires: '1Y' });
    this.$cookie.set('stringSuffixM', 'One month later', { expires: '1M' });
    this.$cookie.set('stringSuffixD', 'One day later', { expires: '1D' });
    this.$cookie.set('stringSuffixh', 'One hour later', { expires: '1h' });
    this.$cookie.set('stringSuffixm', 'Ten minutes later', { expires: '10m' });
    this.$cookie.set('stringSuffixs', 'Thirty seconds later', { expires: '30s' });
    

    3.3 网上找到的语法

    this.$cookies.set(keyName, value[, expireTimes[, path[, domain[, secure]]]]) //return this
    this.$cookies.get(keyName) // return value
    this.$cookies.remove(keyName [, path [, domain]]) // return false or true , warning: next version return this; 
    use isKey(keyname) return true/false,please
    

    参考

    https://github.com/alfhen/vue-cookie

    相关文章

      网友评论

          本文标题:添加vue-cookie

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