zoukankan      html  css  js  c++  java
  • vue----js-cookie的使用方法

    一、安装

    npm install js-cookie --save

    二、引用

    import Cookies from 'js-cookie'

    三、一般使用

    1. 存到Cookie去
    复制代码
    // Create a cookie, valid across the entire site:
    Cookies.set('name', 'value');
    
    // Create a cookie that expires 7 days from now, valid across the entire site:
    Cookies.set('name', 'value', { expires: 7 });
    
    // Create an expiring cookie, valid to the path of the current page:
    Cookies.set('name', 'value', { expires: 7, path: '' });
    复制代码

    2.在Cookie中取出

    复制代码
    // Read cookie:
    Cookies.get('name'); // => 'value'
    Cookies.get('nothing'); // => undefined
    
    // Read all visible cookies:
    Cookies.get(); // => { name: 'value' }
    复制代码

    3.删除

    复制代码
    // Delete cookie:
    Cookies.remove('name');
    
    // Delete a cookie valid to the path of the current page:
    Cookies.set('name', 'value', { path: '' });
    Cookies.remove('name'); // fail!
    Cookies.remove('name', { path: '' }); // removed!
    复制代码

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

    复制代码
    跟一般使用不同的是,从Cookie中取出的时候,要从字符串转换成json格式:
    
    const user = {
      name: 'lia',
      age: 18
    }
    Cookies.set('user', user)
    const liaUser = JSON.parse(Cookies.get('user'))
    复制代码
  • 相关阅读:
    Day4 0708
    Day2 0706
    两道递推公式题的解题报告
    博客还需优化
    飞行路线Luogu4568
    堆优化Dijkstra(Luogu 4779)
    2019四等奖的清明节征文
    2019四等奖的叶圣陶初稿
    Luogu P1072 Hankson的趣味题
    Loj10022 埃及分数(迭代加深搜索IDDFS)
  • 原文地址:https://www.cnblogs.com/tzwbk/p/13260872.html
Copyright © 2011-2022 走看看