VUE学习笔记(十一)-登录和状态管理

登录和状态管理

src/auth/views/UserLogin.vue

<template>
    <div class="login">
        <div class="body">
            <div class="container">
                <h2>用户登陆</h2>
                <el-form :model="ruleForm" ref="loginForm" class="login-form" :rules="rules">
                    <el-form-item label="账号" prop="email">
                        <el-input v-model="ruleForm.email" />
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="ruleForm.password" />
                    </el-form-item>
                    <el-button style="width: 100%;" type="primary" @click="submitForm(loginForm)">登陆</el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script setup>
import { reactive, toRefs, ref } from 'vue';
import { useStore } from "vuex";
const store=useStore();
const loginForm = ref()
const state = reactive({
    ruleForm: {
        email: "ant@163.com",
        password: "ant123"
    }
})

const rules = reactive({
    email: [
        { required: true, message: '请输入账号', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
})

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      //验证通过进行登陆
      store.dispatch('authModule/userLoginAction',state.ruleForm)
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const { ruleForm } = toRefs(state)
</script>
<style scoped>
.login {
    background: url("../../assets/bg.jpg");
    height: 100%;
    width: 100%;
    position: fixed;
    background-size: cover;
}

.body {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20%;
}

.container {
    width: 420px;
    height: 250px;
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    border-radius: 10px;
    border-radius: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
    font-size: 12px;
}
</style>

src/store/auth/index.js

import { loginUser,logOut } from "@/auth/auth.service";
import router from '@/router'
const authModule = {
  //namespaced告诉使用者调用时需要加上命名空间
  namespaced: true,
  state: {
    //全局变量
    signInState:{
        email:'',
        exp:Date.now(),
        sub:"",
        token:null
    }
  },
  getters: {
    //全局变量的计算属性
  },
  mutations: {
    //修改全局变量
    //修改token状态
    userLogin(state,token){
        state.signInState.token=token
        localStorage.setItem("tokenAnt",token)
    }
  },
  actions: {
    //异步修改全局变量
    async userLoginAction({commit},login){
       const {data}= await loginUser(login)
       console.log(data)
       commit('userLogin',data.token)
       router.replace('/')
    },
    logout(){
        //移除token
        logOut();
        //重置路由
    }
  },
};
export default authModule;

src/store/index.js

import { createStore } from 'vuex'
import authModule from './auth'
export default createStore({
  state: {
    //全局变量
  },
  getters: {
    //全局变量的计算属性
  },
  mutations: {
    //修改全局变量
  },
  actions: {
    //异步修改全局变量
  },
  modules: {
    //模块化
    authModule,
  }
})

 

posted on 2024-05-29 10:43  御行所  阅读(13)  评论(0编辑  收藏  举报

导航