Vue3 跳转另一个页面或弹框
1、主页面
<template>
<div class="app-container">
<el-row :gutter="20">
<el-col :span="20" :xs="24">
<el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
<el-form-item label="权限名称">
<el-input
v-model="queryParams.map.saName"
placeholder="请输入权限名称"
clearable
style="width: 240px"
/>
</el-form-item>
<el-form-item label="权限Code">
<el-input
v-model="queryParams.map.saCode"
placeholder="请输入Code"
clearable
style="width: 240px"
/>
</el-form-item>
<el-form-item label="所属菜单">
<el-tree-select placeholder="请选择菜单" v-model="queryParams.map.saSmId" :data="menuOptions"
:render-after-expand="false" clearable/>
</el-form-item>
<el-form-item>
<el-button :color="global.btnColor" icon="Search" @click="handleQuery" v-if="proxy.$checkAuth('SYS_AuthSelect')">搜索</el-button>
<el-button icon="Refresh" @click="handleReset" v-if="proxy.$checkAuth('SYS_AuthReset')">重置</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button :color="global.btnColor" plain icon="Plus" @click="handleAdd" v-if="proxy.$checkAuth('SYS_AuthAdd')">新增</el-button>
</el-col>
</el-row>
<handle-auth ref="handleRef" @getAuthList="getAuthList"></handle-auth>
<div class="tbMain">
<el-table :data="authList" border stripe style="width: 100%"
:header-cell-style="{'background-color':'#4991DD','color':'#ffffff'}" empty-text="无数据">
<el-table-column prop="saName" label="权限名称" align="center"/>
<el-table-column prop="saCode" label="权限Code" align="center"/>
<el-table-column prop="menuName" label="所属菜单" align="center"/>
<el-table-column prop="saNote" label="备注" align="center"/>
<el-table-column prop="saIsuse" label="是否使用" align="center">
<template #default="scope">
{{ scope.row.saIsuse ? '是' : '否' }}
</template>
</el-table-column>
<el-table-column label="操作" width="200px" align="center">
<template #default="scope">
<el-tooltip content="修改" placement="top" v-if="proxy.$checkAuth('SYS_AuthEdit')">
<el-button :color="global.btnColor" circle plain size="small" icon="Edit"
@click="handleEdit(scope.row.saId)"></el-button>
</el-tooltip>
<el-tooltip content="删除" placement="top" v-if="proxy.$checkAuth('SYS_AuthDelete')">
<el-button type="danger" circle plain size="small" icon="Delete"
@click="handleDelete(scope.row.saId)"></el-button>
</el-tooltip>
</template>
</el-table-column>
</el-table>
<el-pagination style="float: right;margin-top: 10px;" background layout="total, prev, pager, next" :locale="zhCn"
small :current-page="curPage" :page-size="queryParams.pageSize" :total="total"
@current-change="curChange"/>
</div>
</div>
</template>
<script lang="ts" setup>
import {ElMessage, ElMessageBox} from 'element-plus'
import {getCurrentInstance, inject, onMounted, ref} from 'vue';
import zhCn from 'element-plus/lib/locale/lang/zh-cn';
import HandleAuth from "./handleAuth.vue";
const global: any = inject('global');
const authList = ref([]);
const total = ref(0);
const handleRef = ref();
const dateRange = ref([]);
const curPage = ref(1);
const queryParams = ref({
pageNum: 1,
pageSize: 10,
map: {
saName: "",
saCode: "",
saSmId: ""
}
});
const {proxy} = getCurrentInstance()
const menuOptions = ref([]);
const emit = defineEmits(['routerView'])
onMounted(() => {
global.axiosUtil.ajax_get(global.baseUrl + "/systemapi/sysMenu/getMenuDownList", function (res) {
menuOptions.value = res.data;
});
getAuthList();
});
const getAuthList = () => {
global.axiosUtil.ajax_post(global.baseUrl + "/systemapi/sysAuth/getAuthList", queryParams.value, function (res: any) {
authList.value = res.data
total.value = res.recordsTotal
});
}
const handleAdd = () => {
handleRef.value.handleAdd();
}
const handleEdit = (value: string) => {
handleRef.value.handleEdit(value);
}
const handleDelete = (value) => {
ElMessageBox.confirm(
'请确定是否删除当前用户数据?',
'提示',
{
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
global.axiosUtil.ajax_get(global.baseUrl + "/systemapi/sysAuth/deleteAuthBySaId/" + value, function (res) {
if (res.altStr == '1') {
ElMessage.success('删除成功!')
getAuthList()
} else {
getAuthList()
}
});
}).catch(() => {
ElMessage({
type: 'info',
message: '删除取消',
})
})
}
const handleQuery = () => {
queryParams.value.pageNum = 1;
getAuthList()
}
const handleReset = () => {
curPage.value = 1
queryParams.value.pageNum = 1;
queryParams.value.map = {
saName: "",
saCode: "",
saSmId: ""
}
dateRange.value = []
getAuthList()
}
const curChange = (currentPage) => {
curPage.value = currentPage
if (curPage.value == 1) {
queryParams.value.pageNum = 0
} else {
queryParams.value.pageNum = (curPage.value - 1) * queryParams.value.pageSize
}
getAuthList()
}
</script>
<style scoped>
.tbMain {
margin-top: 8px;
}
</style>
- 主页面重要的是引用,主要需要下面四点引用
<handle-auth ref="handleRef" @getAuthList="getAuthList"></handle-auth>
<el-button :color="global.btnColor" circle plain size="small" icon="Edit" @click="handleEdit(scope.row.saId)"></el-button>
import HandleAuth from "./handleAuth.vue";
const handleEdit = (value: string) => {
handleRef.value.handleEdit(value);
}
2、子页面
<template>
<el-dialog v-model="handleDialogVisible" :title="titleName" width="30%" destroy-on-close draggable
:close-on-click-modal="false">
<el-form :model="formData" :rules="rules" ref="ruleFormRef" label-width="90px">
<el-row>
<el-col :span="12">
<el-form-item label="权限名称" prop="saName">
<el-input v-model="formData.saName" clearable/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="权限Code" prop="saCode">
<el-input v-model="formData.saCode" clearable :disabled="formData.saId != null"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="所属菜单" prop="saSmid">
<el-tree-select v-model="formData.saSmid" :data="menuOptions" :render-after-expand="false" clearable/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="是否使用">
<el-switch v-model="formData.saIsuse"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="排序" prop="saSort">
<el-input-number v-model="formData.saSort" class="mx-4" :min="0"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="备注">
<el-input v-model="formData.saNote" type="textarea" clearable/>
</el-form-item>
</el-col>
</el-row>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button :color="global.btnColor" @click="submitInfo(ruleFormRef)">确 定</el-button>
<el-button @click="cancelDialog(false)">取 消</el-button>
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import {ref, reactive, inject, onMounted, toRefs, defineProps, defineExpose} from 'vue';
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage, ElMessageBox} from 'element-plus'
const global: any = inject('global');
const emit = defineEmits(['getAuthList'])
const handleDialogVisible = ref(false);
const ruleFormRef = ref<FormInstance>();
const titleName = ref('');
const formData = ref({
saId: null,
saName: null,
saCode: null,
saSmid: null,
saSort: 0,
saNote: "",
saIsuse: true
});
const handleAdd = () => {
handleDialogVisible.value = true
titleName.value = '添加权限'
formData.value = {
saId: null,
saName: null,
saCode: null,
saSmid: null,
saSort: 0,
saNote: "",
saIsuse: true
}
}
const menuOptions = ref([]);
onMounted(() => {
global.axiosUtil.ajax_get(global.baseUrl + "/systemapi/sysMenu/getMenuDownList", function (res) {
menuOptions.value = res.data;
});
});
const submitInfo = async (formEl: FormInstance | undefined) => {
if (!formEl) return
await formEl.validate((valid, fields) => {
if (valid) {
if (formData.value.saId != undefined && formData.value.saId != null) {
global.axiosUtil.ajax_post(global.baseUrl + "/systemapi/sysAuth/updateAuth", formData.value, function (res) {
if (res.altStr == "1") {
ElMessage.success('修改成功!')
emit("getAuthList");
handleDialogVisible.value = false
}
});
} else {
global.axiosUtil.ajax_post(global.baseUrl + "/systemapi/sysAuth/addAuth", formData.value, function (res) {
if (res.altStr == "-1") {
ElMessage.error('权限Code已经存在!')
} else if (res.altStr == "1") {
ElMessage.success('添加成功!')
emit("getAuthList");
handleDialogVisible.value = false
}
});
}
} else {
ElMessage.error('请输入正确后再提交信息!')
}
})
};
const handleEdit = (value: string) => {
handleDialogVisible.value = true
titleName.value = '编辑权限'
global.axiosUtil.ajax_get(global.baseUrl + "/systemapi/sysAuth/getAuthDetailBySaId/" + value, function (res) {
formData.value = res.data
});
};
const rules = reactive<FormRules>({
saName: [
{required: true, message: '请输入权限名', trigger: 'blur'},
],
saCode: [
{required: true, message: '请输入权限Code', trigger: 'blur'},
],
saSmid: [
{required: true, message: '请选择所属菜单', trigger: 'change'},
],
saSort: [
{required: true, message: '请输入排序', trigger: 'blur'},
],
})
const cancelDialog = (value: boolean) => {
handleDialogVisible.value = value
formData.value = {
saId: null,
saName: null,
saCode: null,
saSmid: null,
saSort: 0,
saNote: "",
saIsuse: true
}
ruleFormRef.value.clearValidate()
}
defineExpose({
handleDialogVisible,
handleAdd,
handleEdit,
cancelDialog
});
</script>
<style scoped>
</style>
- 子页面主要需要定义对应变量以及方法
const handleDialogVisible = ref(false);
const handleAdd = () => {
handleDialogVisible.value = true
titleName.value = '添加权限'
formData.value = {
saId: null,
saName: null,
saCode: null,
saSmid: null,
saSort: 0,
saNote: "",
saIsuse: true
}
}
const handleEdit = (value: string) => {
handleDialogVisible.value = true
titleName.value = '编辑权限'
global.axiosUtil.ajax_get(global.baseUrl + "/systemapi/sysAuth/getAuthDetailBySaId/" + value, function (res) {
formData.value = res.data
});
};
const cancelDialog = (value: boolean) => {
handleDialogVisible.value = value
formData.value = {
saId: null,
saName: null,
saCode: null,
saSmid: null,
saSort: 0,
saNote: "",
saIsuse: true
}
ruleFormRef.value.clearValidate()
}
defineExpose({
handleDialogVisible,
handleAdd,
handleEdit,
cancelDialog
});
直接可用,简单增删改列表+弹框。