vue如何优雅的获取Cookie(js-cookie插件)

vue如何优雅的获取Cookie(js-cookie插件)

一, 安装

npm install js-cookie --save

二, 引用

import Cookies from 'js-cookie'

三, 使用

先上代码

import Cookies from 'js-cookie'

const AccessTokenKey = 'accessToken'
//拿到token
const RefreshTokenKey = 'refreshToken'
//刷新token
export function setAccessToken (token) {
  Cookies.set(AccessTokenKey, token)
}

export function setRefreshToken (token) {
  Cookies.set(RefreshTokenKey, token)
}

export function getAccessToken () {
  const cookie = Cookies.get(AccessTokenKey)
  return cookie
}

export function getRefreshToken () {
  return Cookies.get(RefreshTokenKey)
}

export function removeAccessToken () {
  Cookies.remove(AccessTokenKey)
}

export function removeRefreshToken () {
  Cookies.remove(RefreshTokenKey)
}
  1. 存token
    Cookies.set(AccessTokenKey, token);
    Cookies.set(RefreshTokenKey, token)

  2. 取token
    Cookies.get(AccessTokenKey)
    Cookies.get(AccessTokenKey)

Cookies.get('name'); // => 'value'
Cookies.get(); // => { name: 'value' }
  1. 删除token
    Cookies.remove(RefreshTokenKey)

你可能感兴趣的:(js)