Foreword:
Use js-cookie in vue to realize the storage management of cookies.
vue3:
1. Installation
npm install --save js-cookie
2. Create a new configuration file unit.ts
import Cookies from 'js-cookie'
// cookie保存的天数
export const TOKEN_KEY = 'NOW_TOKEN'
export const setToken = (token:string, cookieExpires:any) => {
Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}
export const getToken = () => {
const token = Cookies.get(TOKEN_KEY)
if (token) return token
else return false
}
export const delToken = () => {
Cookies.remove(TOKEN_KEY)
}
3. Specific use
import { getToken, delToken } from './util'
view3+ts:
1. Installation
npm install --save js-cookie
npm install --save @types/js-cookie
2. Create a new configuration file unit.ts
import Cookies from 'js-cookie'
// cookie保存的天数
export const TOKEN_KEY = 'NOW_TOKEN'
export const setToken = (token:string, cookieExpires:any) => {
Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}
export const getToken = () => {
const token = Cookies.get(TOKEN_KEY)
if (token) return token
else return false
}
export const delToken = () => {
Cookies.remove(TOKEN_KEY)
}
3. Specific use
import { getToken, delToken } from './util'