vue-element-admin实现登录和注销操作
登录-获取token令牌操作实现
src/views/login/index.vue
<template>
<div class="login-container">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">立即登录</el-button>
</el-form>
</div>
</template>
注意:点登陆触发 handleLogin 方法
methods: {
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
console.log("点击登陆按钮")
this.$store.dispatch('user/login', this.loginForm).then(() => {
console.log("登录成功");
this.$router.push({
path: this.redirect || '/' });
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
}
}
注意:this.$store.dispatch('user/login', this.loginForm)
会触发vuex中的user/login
src/store/modules/user.js
const actions = {
login({
commit }, userInfo) {
const {
username, password } = userInfo
return new