- NPM 安装
npm install js-cookie --save
复制代码
- 引入
import Cookies from 'js-cookie'
复制代码
- 设置cookie,创建一个cookie,在整个站点有效:
cookies.set('name','value')
复制代码
或 创建一个过期的cookie,该cookie对当前页的路径有效:
cookies.set('name','value',{expires:7,path:'})
复制代码
- 获取cookie:
cookies.get('name');//=>'值'
复制代码
- 读取所有可见的Cookie:
cookies.get();//=>{name:'value'}
复制代码
- 删除Cookie:
cookies.remove('name');
复制代码
- 删除对当前页路径有效的cookie:
cookies.set('name','value',{path:'});
cookies.remove('name');//失败!
cookies.remove('name',{path:'});//remove!
复制代码
Forexample:
<template>
<div class="login-wrap">
<div class="ms-login">
<div class="ms-title">后台管理系统</div>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
<el-form-item prop="username">
<el-input v-model="ruleForm.username" placeholder="请输入用户名">
<el-button slot="prepend" icon="el-icon-lx-people"></el-button>
</el-input>
</el-form-item>
<el-form-item prop="password">
<el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"
@keyup.enter.native="submitForm('ruleForm')">
<el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
</el-input>
</el-form-item>
<div class="login-btn">
<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
</div>
</el-form>
</div>
</div>
</template>
复制代码
<script>
export default {
data: function () {
return {
ruleForm: {
username: '',
password: ''
},
rules: {
username: [
{required: true, message: '请输入用户名', trigger: 'blur'}
],
password: [
{required: true, message: '请输入密码', trigger: 'blur'}
]
}
}
},
//页面加载调用获取cookie值
mounted() {
this.getCookie();
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.$axios.post('admin/login',this.$qs.stringify(this.ruleForm))
.then((res) => {
console.log(res);
if (res.data.code == 0) {
localStorage.setItem('ms_username', this.ruleForm.username);
//保存用户名和密码到cookie
this.setCookie(this.ruleForm.username,this.ruleForm.password,7);
//跳转
this.$router.push('/');
this.$message.success(res.data.message);
} else {
console.log(res.data.message);
this.$message.warning(res.data.message);
}
}).catch((res) => {
console.log(res);
});
} else {
console.log('error submit!!');
return false;
}
});
},
//设置cookie
setCookie(c_name, c_pwd, exdays) {
var exdate = new Date(); //获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
//字符串拼接cookie
window.document.cookie = "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
window.document.cookie = "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
},
//读取cookie
getCookie: function() {
if (document.cookie.length > 0) {
var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('='); //再次切割
//判断查找相对应的值
if (arr2[0] == 'userName') {
this.ruleForm.username = arr2[1]; //保存到保存数据的地方
} else if (arr2[0] == 'userPwd') {
this.ruleForm.password = arr2[1];
}
}
}
},
}
}
</script>
作者:Elvira411
链接:https://juejin.im/post/5dba3783e51d4529e009fece
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。