在GitHub上有很多写好的模板,这个项目也是基于模板做的。
现在记录一下我做的过程
1、修改config文件夹里的dev.env.js里的BASE_API,把地址改成请求后端的公共部分
BASE_API: '"http://192.168.xx.xx"',
2、接下来就是操作src文件,先在 views里写好vew组件(login.vue,regist.vue),写好到router里的index.js里配置好路径
login.vue
<template> <div> <el-form autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px"> <h3>登录</h3> <el-form-item prop="name"> <span> <svg-icon icon-class="user"/> </span> <el-input disabled type="text" v-model="loginForm.name" autoComplete="on" placeholder="用户名"/> </el-form-item> <el-form-item prop="password"> <span> <svg-icon icon-class="password"></svg-icon> </span> <el-input :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="密码"></el-input> <span @click="showPwd"><svg-icon icon-class="eye"/></span> </el-form-item> <el-form-item> <el-button type="primary" :loading="loading" @click.native.prevent="handleLogin"> 登录 </el-button> </el-form-item> </el-form> </div> </template> <script> export default { name: 'login', data() { const validateUsername = (rule, value, callback) => { if (value.trim().length<1) { callback(new Error('用户名不能为空')) } else { callback() } }; const validatePass = (rule, value, callback) => { if (value.trim().length < 1) { callback(new Error('密码不能为空')) } else { callback() } }; return { loginForm: { name: '', password: '' }, loginRules: { name: [{required: true, trigger: 'blur', validator: validateUsername}], password: [{required: true, trigger: 'blur', validator: validatePass}] }, loading: false, pwdType: 'password' } }, methods: { showPwd() { if (this.pwdType === 'password') { this.pwdType = '' } else { this.pwdType = 'password' } }, handleLogin() { this.$refs.loginForm.validate(valid => { if (valid) { this.loading = true; this.$store.dispatch('Login', this.loginForm).then(() => { this.loading = false; this.$router.push({path: 'https://www.jb51.net/'}); }).catch((e) => { this.loading = false }) } else { console.log('error submit!!') return false } }) } } } </script>
router/index.js
{ path: '/login', component: _import('Login/login'), hidden: true }
3、在api里的login.js里写好与后端对接的接口地址,在stores里的modules里的user.js里定义user,有state,mutations,action,具体的请求操作写在action里,login.vue或regist.vue调用user.js里写好的请求,getter.js里定义store的getters
api/login.js
import request from '@/utils/request' export function login(name,password) { return request({ url: '/XX/XX', method: 'post', data: { name, password } }) }
stores/modules/user.js
import { login,regist,logout } from '@/api/login' import { getToken,setToken } from '@/utils/auth' const user = { state: { name:'', token:'' }, mutations: { SET_NAME: (state, name) => { state.name = name; }, SET_TOKEN: (state, token) => { state.token = token; setToken(token); } }, actions: { // 登录 Login({ commit }, userInfo) { const name = userInfo.name.trim(); const password = userInfo.password.trim(); return new Promise((resolve, reject) => { login(name, password).then(response => { const data = response.data; commit('SET_NAME', data.name); commit('SET_TOKEN', data.token); setName(data.name); setToken(data.token); resolve(response); }).catch(error => { reject(error) }) }) }, // 注册 Regist({ commit }, userInfo) { const name= userInfo.name.trim(); const password = userInfo.password.trim(); return new Promise((resolve, reject) => { regist(name, password).then(response => { const data = response.data; commit('SET_NAME', data.name); commit('SET_TOKEN', data.token); setName(data.name);setToken(data.token); resolve(response); }).catch(error => { reject(error) }) }) }, // 登出 LogOut({ commit, state }) { return new Promise((resolve, reject) => { logout().then(response => { commit('SET_NAME', ''); commit('SET_TOKEN', ''); setName(''); setToken(false); //removeName(); //removeToken(); resolve(response); }).catch(error => { reject(error) }) }) }, // 前端 登出 FedLogOut({ commit }) { return new Promise(resolve => { setToken(false); commit('SET_TOKEN', false); resolve() }) } } } export default user
getter.js:
const getters={ name:state=>state.user.name, token:state=>state.user.token } export default getter
4、在utils里的auth.js里对cookies进行操作,写入,读取,删除用户权限,是否登录的标志等