3.6 使用Element UI美化页面
index.vue
<template> <div> <el-menu class="el-menu-demo" mode="horizontal" :router="true"> <el-menu-item index="/selectAll">全部学生</el-menu-item> <el-menu-item index="/insert">添加学生</el-menu-item> <el-menu-item index="/selectOne">查看学生</el-menu-item> <el-menu-item index="/update">修改学生</el-menu-item> </el-menu> <router-view></router-view> </div> </template> <script> export default { name: "index" } </script> <style scoped> </style> 复制代码
insert.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="姓名" prop="pass"> <el-input type="text" v-model="ruleForm.name" ></el-input> </el-form-item> <el-form-item label="年龄" prop="checkPass"> <el-input type="text" v-model="ruleForm.age" ></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button> </el-form-item> </el-form> </div> </template> <script> export default { name: "insert", data() { return { ruleForm: { name: '', age: '' } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert('submit!'); } else { console.log('error submit!!'); return false; } }); }, } } </script> <style scoped> </style> 复制代码
selectOne.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="ID" prop="pass"> <el-input type="text" v-model="ruleForm.id"></el-input> </el-form-item> <el-form-item label="姓名" prop="pass"> <el-input type="text" v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="checkPass"> <el-input type="text" v-model="ruleForm.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button> <el-button @click="resetForm('ruleForm')">重置</el-button> </el-form-item> </el-form> </div> </template> <script> export default { name: "selectOne", data() { return { ruleForm: { id: '', name: '', age: '' } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert('submit!'); } else { console.log('error submit!!'); return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); } } } </script> <style scoped> </style> 复制代码
selectAll.vue
<template> <div> <template> <el-table :data="tableData" style="width: 60%;margin-top:30px;"> <el-table-column prop="id" label="ID" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="age" label="年龄"> </el-table-column> <el-table-column label="操作"> <template> <el-button type="warning" size="small">修改</el-button> <el-button type="danger" size="small">删除</el-button> </template> </el-table-column> </el-table> </template> </div> </template> <script> export default { name: "selectAll", data() { return { tableData: [] } } } </script> <style scoped> </style> 复制代码
update.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="ID" prop="pass"> <el-input type="text" v-model="ruleForm.id"></el-input> </el-form-item> <el-form-item label="姓名" prop="checkPass"> <el-input type="text" v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input type="text" v-model="ruleForm.age"></el-input> </el-form-item> <el-form-item> <el-button type="warning" @click="submitForm('ruleForm')">修改</el-button> </el-form-item> </el-form> </div> </template> <script> export default { name: "update", data() { return { ruleForm: { id: '', name: '', age: '' } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert('submit!'); } else { console.log('error submit!!'); return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); } } } </script> <style scoped> </style> 复制代码
效果
网络异常,图片无法展示
|
网络异常,图片无法展示
|
3.7 整合axios与Spring Boot后端交互
npm install axios --save 复制代码
insert.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="姓名" prop="pass"> <el-input type="text" v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="checkPass"> <el-input type="text" v-model="ruleForm.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm()">提交</el-button> </el-form-item> </el-form> </div> </template> <script> import axios from 'axios' export default { name: "insert", data() { return { ruleForm: { name: '', age: '' } }; }, methods: { submitForm() { axios.post("http://localhost:8081/student/save", this.ruleForm).then(function (resp) { console.log(resp) }) }, } } </script> <style scoped> </style> 复制代码
selectOne.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="ID" prop="pass"> <el-input type="text" v-model="ruleForm.id"></el-input> </el-form-item> <el-form-item label="姓名" prop="pass"> <el-input type="text" v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="checkPass"> <el-input type="text" v-model="ruleForm.age"></el-input> </el-form-item> </el-form> </div> </template> <script> import axios from "axios"; export default { name: "selectOne", data() { return { ruleForm: { id: '', name: '', age: '' } }; }, methods: { getStudent() { const _this = this; axios.get("http://localhost:8081/student/findById/" + this.$route.query.id).then(function (resp) { _this.ruleForm = resp.data; }) } }, created() { this.getStudent(); } } </script> <style scoped> </style> 复制代码
selectAll.vue
<template> <div> <template> <el-table :data="tableData" style="width: 60%;margin-top:30px;"> <el-table-column prop="id" label="ID" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="age" label="年龄"> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="primary" size="small" @click="select(scope.row)">查看</el-button> <el-button type="warning" size="small" @click="update(scope.row)">修改</el-button> <el-button type="danger" size="small" @click="remove(scope.row)">删除</el-button> </template> </el-table-column> </el-table> </template> </div> </template> <script> import axios from "axios"; export default { name: "selectAll", data() { return { tableData: [] } }, methods: { getData() { const _this = this; axios.get("http://localhost:8081/student/findAll").then(function (resp) { _this.tableData = resp.data; }) }, remove(stu) { const _this = this; if (confirm("确定删除吗?")) { axios.delete("http://localhost:8081/student/remove/" + stu.id).then(function (resp) { if (resp.data == 1) { _this.getData(); } }) } }, select(stu) { this.$router.push({ path: "/selectOne", query:{ id: stu.id } }) }, update(stu) { this.$router.push({ path: "/update", query:{ id: stu.id } }) } }, created() { this.getData(); } } </script> <style scoped> </style> 复制代码
update.vue
<template> <div> <el-form :model="ruleForm" status-icon label-width="100px" class="demo-ruleForm" style="margin-top:30px;width: 30%;"> <el-form-item label="ID"> <el-input type="text" v-model="ruleForm.id" disabled></el-input> </el-form-item> <el-form-item label="姓名"> <el-input type="text" v-model="ruleForm.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input type="text" v-model="ruleForm.age"></el-input> </el-form-item> <el-form-item> <el-button type="warning" @click="submitForm()">修改</el-button> </el-form-item> </el-form> </div> </template> <script> import axios from "axios"; export default { name: "update", data() { return { ruleForm: { id: '', name: '', age: '' } }; }, methods: { submitForm() { axios.post("http://localhost:8081/student/update", this.ruleForm).then(function (resp) { console.log(resp) }) }, getStudent() { const _this = this; axios.get("http://localhost:8081/student/findById/" + this.$route.query.id).then(function (resp) { _this.ruleForm = resp.data; }) } }, created() { this.getStudent(); } } </script> <style scoped> </style> 复制代码
4 总结
网络异常,图片无法展示
|