vue3小案例-信息系统
第一步,拉取或者创建一个含有 axios 插件的 vue3 项目
创建项目教程:vue3项目搭建-1-创建项目-CSDN博客
安装 axios 插件命令:
npm install axios
第二步,mock 模拟后端接口
在根目录创建 mock 文件夹,在文件夹中创建 index.js 文件,文件内容:
import Mock from "mockjs"
// 使用 Mock 模拟后端数据
const arr = []
for (let i = 0; i < 10; i++) {
arr.push({
id: Mock.mock("@id"),
name: Mock.mock("@cname"),
place: Mock.mock("@county(true)"),
})
}
export default [
{
url: "/list",
method: "get",
response: () => {
return arr
},
},
{
url: "/del/:id",
method: "delete",
response: (req) => {
const index = arr.findIndex((item) => item.id === req.query.id)
if (index > -1) {
arr.splice(index, 1)
return { success: true }
} else {
return { success: false }
}
},
},
{
url: "/edit/:id",
method: "patch",
response: ({ query, body }) => {
const item = arr.find((item) => item.id === query.id)
if (item) {
item.name = body.name
item.place = body.place
return { success: true }
} else {
return { success: false }
}
},
},
]
vite.config.js:
import { fileURLToPath, URL } from "node:url"
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import { viteMockServe } from "vite-plugin-mock"
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
viteMockServe({
mockPath: "mock",
localEnabled: true,
}),
],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
})
第三步,完善前端代码
App.vue:
<script setup>
import Edit from './components/Edit.vue'
import axios from 'axios'
import { onMounted,ref } from 'vue'
// TODO: 列表渲染
//装数据的列表
const list = ref([])
const getList = async () => {
//通过发送 /list 请求从后端拿到列表数据
const res = await axios.get('/list')
list.value = res.data
}
onMounted(() => getList())
// TODO: 删除功能
//获取 id ,删除 记录,更新页面
const onDelete = async (id) => {
console.log(id)
//将后端中 id 为 {id} 的记录删除,注意这里是左上角 esc 下面的 '`' 符号
await axios.delete(`/del/${id}`)
//更新
getList()
}
// TODO: 编辑功能
//获取 id,修改数据,更新页面
const editRef = ref(null)
const update = (row) => {
editRef.value.open(row)
getList()
}
</script>
<template>
<div class="app">
<el-table :data="list">
<el-table-column label="ID" prop="id"></el-table-column>
<el-table-column label="姓名" prop="name" width="150"></el-table-column>
<el-table-column label="籍贯" prop="place"></el-table-column>
<el-table-column label="操作" width="150">
<template #default="{ row }">
<el-button type="primary" @click="update(row)" link>编辑</el-button>
<el-button type="danger" @click="onDelete(row.id)" link>删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<!-- 将更新页面的监听给子组件 -->
<Edit ref="editRef" @on-update="getList"/>
</template>
<style scoped>
.app {
width: 980px;
margin: 100px auto 0;
}
</style>
修改数据的弹窗表单组件:
<script setup>
// TODO: 编辑
import axios from 'axios'
import { onMounted,ref } from 'vue'
// 弹框开关
const dialogVisible = ref(false)
// 表单关联行中的字段
const form = ref({
name: '',
place: '',
id: ''
})
const open = (row) => {
console.log(row)
form.value.id = row.id
form.value.name = row.name
form.value.place = row.place
dialogVisible.value = true
}
defineExpose({
open
})
//拿到父组件的所有标记
const emit = defineEmits(['on-update'])
//TODO
const editUpdate = async () => {
console.log(form)
//将姓名和籍贯带到路径中,并发送请求
await axios.patch(`/edit/${form.value.id}`, {
name: form.value.name,
place: form.value.place,
})
//关闭弹窗
dialogVisible.value = false
//触发监听,通知父组件更新列表
emit('on-update')
}
</script>
<template>
<el-dialog v-model="dialogVisible" title="编辑" width="400px">
<el-form label-width="50px">
<el-form-item label="姓名">
<el-input placeholder="请输入姓名" v-model="form.name"/>
</el-form-item>
<el-form-item label="籍贯">
<el-input placeholder="请输入籍贯" v-model="form.place"/>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="editUpdate">确认</el-button>
</span>
</template>
</el-dialog>
</template>
<style scoped>
.el-input {
width: 290px;
}
</style>
这些数据都是 mock 随机生成的,第一条是修改后的结果
原文地址:https://blog.csdn.net/m0_75138009/article/details/144044046
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!