总结:通过路由来进行页面跳转
环境配置:
创建一个新的vue:
1.npm create vite:用于创建基于 Vite 构建工具的新项目的命令
2.npm install
3.npm install vue-router //导入路由依赖
npm install axios //安装axios
npm install pinia // 安装pinia
npm install element-plus //安装element-plus
main中的:
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
//导入element-plus相关内容
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import locale from 'element-plus/dist/locale/zh-cn.js'
//引入router
import router from './router/index.js'
/* 引入pinia */
import {createPinia} from 'pinia'
//创建pinia对象
const pinia=createPinia();
createApp(App).use(ElementPlus,{locale}).use(router).use(pinia).mount('#app')
启动命令:
4.npm run dev启动
下面就是我写的一个小demo:
APP.vue:
<script setup>
</script>
<template>
<router-view></router-view>
</template>
Login.vue:
<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const registerData = ref({
username: '',
password: '',
})
import axios from 'axios';
const router = useRouter()
import {useTokenStore} from '../stores/token.js'
const tokenStore=useTokenStore();
const login = async () => {
let result=await axios.post(`http://localhost:8080/user/login`, registerData.value);
//let result = await userLoginService(registerData.value)
console.log(result);
ElMessage.success(result.msg ? result.msg : '登录成功')
tokenStore.setToken(result.data.data)
router.push('/')
}
</script>
<template>
<el-row class="login-page">
<el-col :span="12" class="bg"></el-col>
<el-col :span="6" :offset="3" class="form">
<!-- 登录表单 -->
<el-form ref="form" size="large" autocomplete="off" :rules="rules" :model="registerData">
<h1>登录</h1>
<el-form-item prop="username">
<el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.username"></el-input>
</el-form-item>
<el-form-item prop="password">
<el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
</el-form-item>
<!-- 登录按钮 -->
<el-form-item>
<el-button class="button" type="primary" auto-insert-space @click="login">
登录
</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</template>
<style scoped>
.login-page {
height: 100vh;
background-color: #fff;
}
.bg {
background: url('../assets/shijian.jpg') no-repeat 60% center / 240px auto,
url('../assets/dashijian.jpg') no-repeat center / cover;
border-radius: 0 20px 20px 0;
}
.form {
display: flex;
flex-direction: column;
justify-content: center;
user-select: none;
.title {
margin: 0 auto;
}
.button {
width: 100%;
}
.flex {
width: 100%;
display: flex;
justify-content: space-between;
}
}
</style>
Examine.vue:
<template>
<el-card v-if="table" class="box-card">
<el-form :model="formInline" :size="formSize" ref="ruleFormRef" class="demo-form-inline" :inline="true"
:rules="rules" :label-position="labelPosition" status-icon :label-width="labelWidth">
<el-form-item label="姓名" class="custom-description-item">
<el-input v-model="formInline.name" placeholder="姓名" :disabled="formInline.disabled" clearable />
</el-form-item>
<el-form-item label="性别" style="width: 480px;" :inline="true">
<el-select v-model="formInline.sex" placeholder="性别" :disabled="formInline.disabled" clearable>
<el-option label="男" value="男" />
<el-option label="女" value="女" />
</el-select>
</el-form-item>
<el-form-item label="年龄" class="custom-description-item">
<el-input v-model="formInline.age" placeholder="年龄" :disabled="formInline.disabled" clearable />
</el-form-item>
<el-form-item label="住址" class="custom-description-item">
<el-input v-model="formInline.address" placeholder="住址" :disabled="formInline.disabled" clearable />
</el-form-item>
<el-form-item label="面试岗位" :inline="true">
<el-select v-model="formInline.post" placeholder="面试岗位" :disabled="formInline.disabled" clearable>
<el-option label="开发部" value="开发部" />
<el-option label="研发部" value="研发部" />
<el-option label="测试部" value="测试部" />
<el-option label="运维部" value="运维部" />
</el-select>
</el-form-item>
<el-form-item label="面试日期" :inline="true">
<el-date-picker v-model="formInline.interviewTime" type="datetime" :disabled="formInline.disabled"
placeholder="面试日期" clearable />
</el-form-item>
<el-form-item label="描述" style="width: 990px;">
<el-input type="textarea" v-model="formInline.content" placeholder="描述" :disabled="formInline.disabled"
clearable />
</el-form-item>
<el-form-item label="建议薪资" style="width: 990px;">
<el-input v-model="formInline.suggestedSalary" placeholder="建议薪资" :disabled="formInline.disabled"
clearable />
</el-form-item>
<el-form-item label="领导意见" class="custom-description-item" v-if="text" label-width="80px" prop="opinionRules">
<el-input v-model="formInline.opinion" :disabled="standing" clearable />
</el-form-item>
<el-form-item label="薪资" class="custom-description-item" v-if="text" prop="payRules">
<el-input v-model="formInline.pay" :disabled="standing" clearable />
</el-form-item>
<el-form-item>
<el-button @click="handleCancel">取消</el-button>
<el-button v-if="standing" type="primary" @click="handleSave" :disabled="formInline.disabled">保存</el-button>
<el-button v-if="standing" type="primary" @click="handleSubmit"
:disabled="formInline.disabled">提交</el-button>
<el-button v-if="!standing" type="primary" @click="NotPass">不通过</el-button>
<el-button v-if="!standing" type="primary" @click="SuccessPass">通过</el-button>
</el-form-item>
</el-form>
</el-card>
<el-row class="mb-4" v-if="showButtons">
<el-button v-if="standing" type="primary" @click="add">新增</el-button>
<el-button v-if="standing" type="primary" @click="upExcel">导出</el-button>
</el-row>
<el-table v-if="!table" :data="tableData" style="width: 100%">
<el-table-column fixed prop="name" label="姓名" width="150" />
<el-table-column prop="sex" label="性别" width="120" />
<el-table-column prop="age" label="年龄" width="120" />
<el-table-column prop="address" label="住址" width="120" />
<el-table-column prop="post" label="面试岗位" width="120" />
<el-table-column prop="interviewTime" label="面试时间" width="220" />
<el-table-column prop="content" label="描述" width="220" />
<el-table-column prop="suggestedSalary" label="建议薪资" width="120" />
<el-table-column prop="status" label="审批状态" width="120" />
<el-table-column fixed="right" label="操作" width="120">
<template #default="{ row }">
<el-button v-if="row.status !== '领导审批' && standing" link type="primary" size="small"
@click="updateView(row)">修改</el-button>
<el-button v-else-if="standing" link type="primary" size="small" @click="handleClick(row)">查看</el-button>
<el-button v-if="standing" link type="primary" size="small" @click="deleteView(row)">删除</el-button>
<el-button v-if="!standing" link type="danger" size="small" @click="approve(row)">审批</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" setup>
import { useRouter } from 'vue-router'
const router = useRouter()
import { useTokenStore } from '../stores/token.js'
const tokenStore = useTokenStore();
console.log(tokenStore.token.standing);
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import type { FormInstance, FormRules } from 'element-plus'
// 控制显示领导还是员工的
const standing = ref(false); // 员工领导开关
interface RuleForm {
opinionRules: string
payRules: string
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
opinionRules: '',
payRules: ''
})
const showButtons = ref(true);//显示主页面
const table = ref(false);//显示卡片
const text = ref(false);//显示文本
const isUpdate = ref(false); // 新增/修改标志
const labelPosition = 'left'; // 设置标签位置为左侧
const labelWidth = '75px';// 设置标签的宽度,根据需要调整
// 面试数据
const tableData = ref([])
const formInline = reactive({
id: '',
name: '',
sex: '',
age: '',
address: '',
post: '',
interviewTime: '',
content: '',
suggestedSalary: '',
status: '',
opinion: '',
pay: '',
disabled: false, // 添加这行
// 其他字段...
});
const rules = reactive<FormRules<RuleForm>>({
opinionRules: [
{ required: true, message: '意见不能为空', trigger: 'blur' },
],
payRules: [
{ required: true, message: '薪资不能为空', trigger: 'blur' },
]
})
const user = ref({
id: '',
standing: ''
})
// 解除 opinion 和 pay 字段的禁用
const enableOpinionPay = () => {
formInline.opinion = ''; // 清空 opinion 字段内容
formInline.pay = ''; // 清空 pay 字段内容
};
const NotPass = async () => {
try {
formInline.status = "未通过";
// 请求对应的数据
const response = await axios.put(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData2();
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
}
const SuccessPass = async () => {
try {
formInline.status = "通过";
// 请求对应的数据
const response = await axios.put(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData2();
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
}
//取消
const handleCancel = () => {
showButtons.value = true;
table.value = false;
}
//保存
const handleSave = async () => {
try {
formInline.status = '人事填写';
if (isUpdate.value == true) {
// 请求对应的数据
const response = await axios.post(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData();
} else {
// 请求对应的数据
const response = await axios.put(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData();
}
}
catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
}
// 报表导出
const upExcel = async () => {
try {
// 请求对应的数据
const response = await axios.get(`http://localhost:8080/applicant/excel`);
console.log(response.data.data);
tableData.value = response.data.data;
fetchData();
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
}
// 提交
const handleSubmit = async () => {
try {
formInline.status = '领导审批';
if (isUpdate.value == true) {
// 请求对应的数据
const response = await axios.post(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData();
} else {
// 请求对应的数据
const response = await axios.put(`http://localhost:8080/applicant`, formInline);
console.log(response.data.data);
tableData.value = response.data.data;
table.value = false;
showButtons.value = true;
fetchData();
}
}
catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
}
// 页面初始化
const fetchData = async () => {
try {
// 请求对应的数据
const response = await axios.get(`http://localhost:8080/applicant/list`);
console.log(response.data.data);
tableData.value = response.data.data;
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
};
// 页面初始化2
const fetchData2 = async () => {
try {
// 请求对应的数据
const response = await axios.get(`http://localhost:8080/applicant/list2`);
console.log(response.data.data);
tableData.value = response.data.data;
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
};
const login = async (uid) => {
try {
// 请求对应的数据
const response = await axios.get(`http://localhost:8080/user/login/` + uid);
console.log(response.data.data);
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
};
onMounted(() => {
console.log(tokenStore.token.standing);
if (tokenStore.token.standing == '0') {
standing.value = false;
} else {
standing.value = true;
}
console.log(standing.value);
if (standing.value==false) {
fetchData2() //在领导页面加载时调用fetchData2函数获取数据;
}
if(standing.value==true) {
fetchData(); // 在员工页面加载时调用fetchData函数获取数据;
}
});
const handleClear = () => {
// 清空formInline数据
id: '';
formInline.name = '';
formInline.sex = '';
formInline.age = '';
formInline.address = '';
formInline.post = '';
formInline.interviewTime = '';
formInline.content = '';
formInline.suggestedSalary = '';
formInline.status = '';
formInline.opinion = '';
formInline.pay = '';
// 可根据实际情况添加其他字段的清空操作
};
// 新增
const add = async () => {
table.value = true;
showButtons.value = false;
text.value = false;
handleClear();
formInline.disabled = false; // 解除禁用
isUpdate.value = true;
}
// 处理审批逻辑
const approve = (rowData) => {
table.value = true;
showButtons.value = false;
text.value = true;
// 将点击的数据赋值给表单对象
Object.assign(formInline, rowData);
console.log(formInline);
formInline.disabled = true; // 禁用
};
// 添加处理修改逻辑
const updateView = (rowData) => {
table.value = true;
showButtons.value = false;
text.value = false;
// 将点击的数据赋值给表单对象
Object.assign(formInline, rowData);
console.log(formInline);
formInline.disabled = false; // 解除禁用
isUpdate.value = false;
};
// 添加处理删除逻辑
const deleteView = async (rowData) => {
try {
// 将点击的数据赋值给表单对象
Object.assign(formInline, rowData);
// 请求对应的数据
const response = await axios.delete(`http://localhost:8080/applicant/` + formInline.id);
console.log(response.data.data);
tableData.value = response.data.data;
fetchData();
} catch (error) {
console.error('请求失败', error);
// 处理错误逻辑...
}
};
// 添加处理查看逻辑
const handleClick = (rowData) => {
table.value = true;
showButtons.value = false;
text.value = true;
// 将点击的数据赋值给表单对象
Object.assign(formInline, rowData);
formInline.disabled = true; // 设置为禁用
}
</script>
<style>
.custom-description-item .el-input {
--el-input-width: 400px;
}
.demo-form-inline .el-select {
--el-select-width: 220px;
}
</style>
router/index.js:
// 导入vue-router
import { createRouter, createWebHistory } from 'vue-router'
// 导入组件
import Examine from '../components/Examine.vue';
import Login from '../components/Login.vue';
const routes = [
{
path: '/login',
name:'login',
component: Login,
},
{
path: '/',
name: 'index',
component: Examine
}
]
//创建路由器
const router = createRouter({
history: createWebHistory(),
routes: routes
})
export default router
stores/token.js:
//定义store
import { defineStore } from 'pinia';
import { ref } from 'vue';
// 第一个参数 名字 唯一
// 第二个参数 : 函数
// 返回值 :函数
export const useTokenStore = defineStore(
'token', () => {
//定义状态内容
// 响应式变量
const token = ref('')
// 定义函数,修改token的值
const setToken = (newToken) => {
token.value = newToken
}
// 移除token的值
const removeToken = () => {
token.value = ''
}
return {
token, setToken, removeToken
}
}
)
stores/userinfo.js:
import { defineStore } from 'pinia'
import { ref } from 'vue'
const useUserInfoStore = defineStore('userinfo', () => {
//定义状态相关函数
const info = ref({})
const setInfo = (newInfo) => {
info.value = newInfo
}
const removeInfo = () => {
info.value = {}
}
return { info, setInfo, removeInfo }
}, { persist:true})
export default useUserInfoStore;