zoukankan      html  css  js  c++  java
  • Cookie的使用(js-cookie插件)

    参考网址:https://www.jianshu.com/p/6e1bacd35f59

    一、安装

    npm install js-cookie --save

    二、引用

    import Cookies from 'js-cookie'

    三、一般使用

    1. 存到Cookie去
    1. // Create a cookie, valid across the entire site:  
    2. Cookies.set('name''value');  
    3.    
    4. // Create a cookie that expires 7 days from now, valid across the entire site:  
    5. Cookies.set('name''value', { expires: 7 });  
    6.    
    7. // Create an expiring cookie, valid to the path of the current page:  
    8. Cookies.set('name''value', { expires: 7, path: '' });  
    2.在Cookie中取出
    1. // Read cookie:  
    2. Cookies.get('name'); // => 'value'  
    3. Cookies.get('nothing'); // => undefined  
    4.    
    5. // Read all visible cookies:  
    6. Cookies.get(); // => { name: 'value' }  
    3.删除
    1. // Delete cookie:  
    2. Cookies.remove('name');  
    3.    
    4. // Delete a cookie valid to the path of the current page:  
    5. Cookies.set('name''value', { path: '' });  
    6. Cookies.remove('name'); // fail!  
    7. Cookies.remove('name', { path: '' }); // removed!  

    四、特殊使用(在Cookie中存对象)

    跟一般使用不同的是,从Cookie中取出的时候,要从字符串转换成json格式:
    1. const user = {  
    2. name: 'lia',  
    3. age: 18}  
    4. Cookies.set('user', user)const liaUser = JSON.parse(Cookies.get('user'))  
    1. // Create a cookie, valid across the entire site:  
    2. Cookies.set('name''value');  
    3.    
    4. // Create a cookie that expires 7 days from now, valid across the entire site:  
    5. Cookies.set('name''value', { expires: 7 });  
    6.    
    7. // Create an expiring cookie, valid to the path of the current page:  
    8. Cookies.set('name''value', { expires: 7, path: '' });  
  • 相关阅读:
    codeforces #550D Regular Bridge 构造
    java学习路线-Java技术人员之路从0基础到高级
    iOSeasy造成循引用的场景
    Hybird App(一)----第一次接触
    PNP管理器简析--基于ReactOS0.33
    为什么寄存器比内存快?
    cookie 与 session 的差别、联系
    webstorm 配合IIS使用
    js实现可拉伸移动的div
    无法识别的属性“targetFramework”。请注意属性名称区分大小写
  • 原文地址:https://www.cnblogs.com/zzz-knight/p/11692362.html
Copyright © 2011-2022 走看看