安装
npm install vue3-cookies --save
main.js
import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueCookies from 'vue3-cookies'
createApp(App).use(store).use(router).use(VueCookies).mount('#app')
使用
import {useCookies} from 'vue3-cookies'
const {cookies} = useCookies();
cookies.set("ts", "123123", "10s")
具体
https://github.com/KanHarI/vue3-cookies
plugins/cookie.js
import {useCookies} from "vue3-cookies";
const {cookies} = useCookies();
export const getToken = () => {
return cookies.get("token") || "";
}
export const getUserName = () => {
return cookies.get("username") || "";
}
export const setUserToken = (user, token) => {
cookies.set('username', user, "30s");
cookies.set('token', token, "30s");
}
export const clearUserToken = () => {
cookies.remove("username");
cookies.remove("token");
}
标签:cookies,const,token,export,vue3,import
From: https://www.cnblogs.com/sherwin1995/p/16905461.html