持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第10天,点击查看活动详情
路由守卫
没有登陆跳转到登陆页和禁止多次登陆
permissions.js 页面
import router from './router'
router.beforeEach((to,from,next) => {
//要根据登陆后值判断
const token = false
//因值是写死的,需注释,如果值是真实的,注释去掉
//没登陆跳转登陆页
// if(!token && to.path != "/login"){
// return next({ path: "/login"})
// }
//禁止多次登陆
if(token && to.path == "/login") {
return next({ path:from.path ? from.path : "/"})
}
next()
})
main.js引入
import './permission'
登陆api
在api目录新增1个登陆方法,
export function login(params) {
return request.post("/api/login",params)
}
这里爬了一个小坑,因为传过来的params是reactive创建的,自带有{},如果在返回值设置{params}会多一个{}
集成windicss
为了布局更加快速和方便,引入windicss框架,当前项目使用的vuecli(也支持vite和其它脚手架)脚手架
在项目目录执行
vue add windicss
在vue.config.js配置中加入windicss配置
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
//加入插件
pluginOptions: {
windicss: {
}
}
})
在main.js引入
import 'windi.css'
然后将vscode 关闭重开生效,不重开会报错
使用
在html等元素中定义class, 将windicss代码写在 class里面
例:
flex布局并水平垂直居中,背景白色
class="flex items-center justify-center bg-light-50"
跨域代理配置
默认请求接口时会跨域(浏览器自带的安全策略),在后端nginx或后端项目中配置允许是可以。如果没有后端权限,这里在vue.config.js配置一下代理也是可以的
//在defineConfig方法 增加一个对象配置
devServer: {
proxy: {
'/api': { //baseURL 和axios里配置对应
target: 'https://api.apiopen.top', //后端接口地址
pathRewrite:{'^/api': ''}, //匹配规则 将api重写为空
changeOrigin: true, //变更请求头,让后端以为是自已的地址
ws: false //关闭websocket,如果没有否则Console会一直报错
},
}
}
账号密码没有提供,需要自行到apiopen.top网站注册
登陆页view
登陆页视图和表单校验
<template>
//loginState 待实现,登陆显示其它内容
<el-row class="login-page" v-show="loginState">
<el-col>
<el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="demo-ruleForm" :size="formSize" status-icon>
<div class="text-4xl mx-auto leading-loose">
账号密码登录
</div>
<el-form-item label="账号" prop="account" class="w-[250px] mx-auto">
<el-input v-model="ruleForm.account">
</el-input>
</el-form-item>
<el-form-item label="密码" prop="password" class="w-[250px] mx-auto" >
<el-input type="password" v-model="ruleForm.password" show-password>
</el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm" class="mx-auto" :loading="loading">登录</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { login } from '@/api/openapi'
import { useRouter } from 'vue-router'
const router = useRouter()
const ruleFormRef = ref(null)
const loginState = ref(true)
const formSize = ref('default')
const loading = ref(false)
const ruleForm = reactive({
account: "",
password: ""
})
//element-plus自带的表单校验
const rules = {
account:[
{
required: true,
message: '账号不能为空',
trigger: 'blur'
},
],
password:[
{
required: true,
message: '密码不能为空',
trigger: 'blur'
},
]
}
//登陆成功跳转到 首页
const submitForm = () => {
ruleFormRef.value.validate((valid) =>{
if(!valid){
console.log("false")
}
})
loading.value =true
login(ruleForm).then(res=>{
console.log(res)
router.push("/")
}).finally(()=>{
loading.value = false
})
}
</script>
<style>
.login-page{
@apply min-h-screen flex items-center justify-center bg-light-50;
}
</style>
结语: 至此登陆页面,逻辑等已经完成,但是因为token没有持久化,如果页面关闭等操作,登陆就无效了,下一章来完成持久化等功能