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'//拿到tokenconst RefreshToke
·
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)
}
-
存token
Cookies.set(AccessTokenKey, token);
Cookies.set(RefreshTokenKey, token) -
取token
Cookies.get(AccessTokenKey)
Cookies.get(AccessTokenKey)
Cookies.get('name'); // => 'value'
Cookies.get(); // => { name: 'value' }
- 删除token
Cookies.remove(RefreshTokenKey)
更多推荐
已为社区贡献2条内容
所有评论(0)