安装插件
import Cookies from 'js-cookie'
组件引入
import Cookies from 'js-cookie';
存值:
Cookies.set('username', state.account, { expires: 30 });
// username 存的值的名字,state.account 存的值 expires 存储的时间,30天Cookies.set('password', state.password, { expires: 30 });Cookies.set('rememberMe', state.rememberMe, { expires: 30 });
取值:
const username = Cookies.get('username');const password = Cookies.get('password');const rememberMe = Cookies.get('rememberMe');
登录记住密码的逻辑
function handleLogin() { // 登录if (state.account && state.password) {if (state.rememberMe) { // 记住密码被勾选Cookies.set('username', state.account, { expires: 30 });Cookies.set('password', state.password, { expires: 30 });Cookies.set('rememberMe', state.rememberMe, { expires: 30 });} else { // 记住密码取消勾选// 否则移除Cookies.remove('username');Cookies.remove('password');Cookies.remove('rememberMe');}router.push('/')ElMessage.success('登录成功')} else {ElMessage.warning('请输入账号密码')}};function getCookie() {const username = Cookies.get('username');const password = Cookies.get('password');const rememberMe = Cookies.get('rememberMe');state.account = username === undefined ? state.account : usernamestate.password = password === undefined ? state.password : passwordstate.rememberMe = rememberMe === undefined ? false : Boolean(rememberMe)
};getCookie();