• 前端vue实现讲师新增和修改功能


    1.在router中配置路由菜单

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. // in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
    4. // detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading
    5. Vue.use(Router)
    6. /* Layout */
    7. import Layout from '../views/layout/Layout'
    8. /**
    9. * hidden: true if `hidden:true` will not show in the sidebar(default is false)
    10. * alwaysShow: true if set true, will always show the root menu, whatever its child routes length
    11. * if not set alwaysShow, only more than one route under the children
    12. * it will becomes nested mode, otherwise not show the root menu
    13. * redirect: noredirect if `redirect:noredirect` will no redirect in the breadcrumb
    14. * name:'router-name' the name is used by (must set!!!)
    15. * meta : {
    16. title: 'title' the name show in submenu and breadcrumb (recommend set)
    17. icon: 'svg-name' the icon show in the sidebar,
    18. }
    19. **/
    20. export const constantRouterMap = [
    21. { path: '/login', component: () => import('@/views/login/index'), hidden: true },
    22. { path: '/404', component: () => import('@/views/404'), hidden: true },
    23. {
    24. path: '/',
    25. component: Layout,
    26. redirect: '/dashboard',
    27. name: 'Dashboard',
    28. hidden: true,
    29. children: [{
    30. path: 'dashboard',
    31. component: () => import('@/views/dashboard/index')
    32. }]
    33. },
    34. {
    35. path: '/teacher',
    36. component: Layout,
    37. redirect: '/example/table',
    38. name: '讲师管理',
    39. meta: { title: '讲师管理', icon: 'example' },
    40. children: [
    41. {
    42. path: 'list',
    43. name: '讲师列表',
    44. component: () => import('@/views/teacher/list'),
    45. meta: { title: '讲师列表', icon: 'table' }
    46. },
    47. {
    48. path: 'add',
    49. name: '添加讲师',
    50. component: () => import('@/views/teacher/add'),
    51. meta: { title: '添加讲师', icon: 'tree' }
    52. },
    53. {
    54. path: 'add/:id',
    55. name: 'EduTeacherEdit',
    56. component: () => import('@/views/teacher/add'),
    57. meta: { title: '编辑讲师', noCache: true },
    58. hidden: true
    59. }
    60. ]
    61. },
    62. {
    63. path: '/example',
    64. component: Layout,
    65. redirect: '/example/table',
    66. name: 'Example',
    67. meta: { title: 'Example', icon: 'example' },
    68. children: [
    69. {
    70. path: 'table',
    71. name: 'Table',
    72. component: () => import('@/views/table/index'),
    73. meta: { title: 'Table', icon: 'table' }
    74. },
    75. {
    76. path: 'tree',
    77. name: 'Tree',
    78. component: () => import('@/views/tree/index'),
    79. meta: { title: 'Tree', icon: 'tree' }
    80. }
    81. ]
    82. },
    83. {
    84. path: '/form',
    85. component: Layout,
    86. children: [
    87. {
    88. path: 'index',
    89. name: 'Form',
    90. component: () => import('@/views/form/index'),
    91. meta: { title: 'Form', icon: 'form' }
    92. }
    93. ]
    94. },
    95. {
    96. path: '/nested',
    97. component: Layout,
    98. redirect: '/nested/menu1',
    99. name: 'Nested',
    100. meta: {
    101. title: 'Nested',
    102. icon: 'nested'
    103. },
    104. children: [
    105. {
    106. path: 'menu1',
    107. component: () => import('@/views/nested/menu1/index'), // Parent router-view
    108. name: 'Menu1',
    109. meta: { title: 'Menu1' },
    110. children: [
    111. {
    112. path: 'menu1-1',
    113. component: () => import('@/views/nested/menu1/menu1-1'),
    114. name: 'Menu1-1',
    115. meta: { title: 'Menu1-1' }
    116. },
    117. {
    118. path: 'menu1-2',
    119. component: () => import('@/views/nested/menu1/menu1-2'),
    120. name: 'Menu1-2',
    121. meta: { title: 'Menu1-2' },
    122. children: [
    123. {
    124. path: 'menu1-2-1',
    125. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
    126. name: 'Menu1-2-1',
    127. meta: { title: 'Menu1-2-1' }
    128. },
    129. {
    130. path: 'menu1-2-2',
    131. component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
    132. name: 'Menu1-2-2',
    133. meta: { title: 'Menu1-2-2' }
    134. }
    135. ]
    136. },
    137. {
    138. path: 'menu1-3',
    139. component: () => import('@/views/nested/menu1/menu1-3'),
    140. name: 'Menu1-3',
    141. meta: { title: 'Menu1-3' }
    142. }
    143. ]
    144. },
    145. {
    146. path: 'menu2',
    147. component: () => import('@/views/nested/menu2/index'),
    148. meta: { title: 'menu2' }
    149. }
    150. ]
    151. },
    152. {
    153. path: 'external-link',
    154. component: Layout,
    155. children: [
    156. {
    157. path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
    158. meta: { title: 'External Link', icon: 'link' }
    159. }
    160. ]
    161. },
    162. { path: '*', redirect: '/404', hidden: true }
    163. ]
    164. export default new Router({
    165. // mode: 'history', //后端支持可开
    166. scrollBehavior: () => ({ y: 0 }),
    167. routes: constantRouterMap
    168. })

    2.添加完成路由菜单后,在views文件夹中添加对应的组件,先添加简单的页面元素。这里面添加的组件一定要和上面配置的路由中保持一致。在src文件夹下的views文件夹下创建teacher文件夹,然后在该文件夹下分别创建list、add组件。创建完该组件后首先添加简单的页面元素

    3.添加了简单的页面元素后,就需要在api文件中创建对应的js文件向后端发送请求。此文件命名为teacher.js

    1. import request from '@/utils/request'
    2. export default {
    3. // 带条件的分页查询讲师列表
    4. getTeacherPageVo(current, limit, teacherQuery){
    5. return request({
    6. url: `/eduservice/eduteacher/getTeacherPageVo/${current}/${limit}`,
    7. method: 'post',
    8. data: teacherQuery // 转换成json传递
    9. })
    10. },
    11. // 根据id删除讲师
    12. delTeacher(id){
    13. return request({
    14. url: `/eduservice/eduteacher/${id}`,
    15. method: 'delete'
    16. })
    17. },
    18. // 添加讲师
    19. addTeacher(eduTeacher){
    20. return request({
    21. url: `/eduservice/eduteacher/addTeacher`,
    22. method: 'post',
    23. data: eduTeacher // 转换成json传递
    24. })
    25. },
    26. // 根据id查询讲师
    27. getTeacherById(id){
    28. return request({
    29. url: `/eduservice/eduteacher/getTeacherById/${id}`,
    30. method: 'get'
    31. })
    32. },
    33. // 修改讲师
    34. updateTeacher(eduTeacher){
    35. return request({
    36. url: `/eduservice/eduteacher/updateTeacher`,
    37. method: 'post',
    38. data: eduTeacher // 转换成json传递
    39. })
    40. }
    41. }

     4.发送请求的api创建完成后,就需要对页面从后端获取的数据进行渲染

    list.vue的文件内容如下

    1. <script>
    2. import teacher from "@/api/teacher";
    3. export default {
    4. data() {
    5. return {
    6. current: 1, // 当前页
    7. limit: 10, // 每页显示多少行
    8. teacherQuery: {}, //查询条件
    9. list: [], //数据列表
    10. total: 0
    11. };
    12. },
    13. created() {
    14. this.getTeacherPageQuery();
    15. },
    16. methods: {
    17. // 带条件的分页查询讲师列表
    18. getTeacherPageQuery(current = 1) {
    19. this.current = current;
    20. teacher
    21. .getTeacherPageVo(this.current, this.limit, this.teacherQuery)
    22. .then(response => {
    23. console.log(response);
    24. this.list = response.data.list;
    25. this.total = response.data.total;
    26. });
    27. },
    28. // 清空
    29. resetData() {
    30. this.teacherQuery = {};
    31. this.getTeacherPageQuery();
    32. },
    33. // 删除讲师
    34. removeDataById(id) {
    35. this.$confirm("此操作将永久删除该讲师, 是否继续?", "提示", {
    36. confirmButtonText: "确定",
    37. cancelButtonText: "取消",
    38. type: "warning"
    39. })
    40. .then(() => {
    41. teacher.delTeacher(id).then(response => {
    42. console.log("删除成功...");
    43. this.getTeacherPageQuery();
    44. this.$message({
    45. type: "success",
    46. message: "删除成功!"
    47. });
    48. })
    49. .catch(() => {
    50. this.$message({
    51. type: "info",
    52. message: "已取消删除"
    53. });
    54. });
    55. });
    56. }
    57. }
    58. };
    59. script>

     add.vue的文件内容如下

    1. <script>
    2. import teacher from "@/api/teacher";
    3. export default {
    4. data() {
    5. return {
    6. eduTeacher: {}, //表单数据
    7. saveBtnDisabled: false // 按钮是否不可操作
    8. };
    9. },
    10. created() {
    11. if (this.$route.params && this.$route.params.id) {
    12. console.log("this.$route.params.id:" + this.$route.params.id);
    13. teacher.getTeacherById(this.$route.params.id).then(response => {
    14. this.eduTeacher = response.data.eduTeacher;
    15. });
    16. } else {
    17. }
    18. },
    19. methods: {
    20. saveOrUpdate() {
    21. if(this.eduTeacher.id){
    22. // 如果id存在就进行修改操作
    23. this.updateTeacherInfo()
    24. }else{
    25. // 如果id不存在就进行添加操作
    26. this.addTeacher()
    27. }
    28. },
    29. // 添加讲师
    30. saveTeacher() {
    31. teacher.addTeacher(this.eduTeacher).then(response => {
    32. // 提示成功
    33. this.$message({
    34. type: "success",
    35. message: "添加成功!"
    36. });
    37. // 路由跳转
    38. this.$router.push({ path: "/teacher/list" });
    39. });
    40. },
    41. // 修改讲师
    42. updateTeacherInfo() {
    43. teacher.updateTeacher(this.eduTeacher).then(response => {
    44. // 提示成功
    45. this.$message({
    46. type: "success",
    47. message: "修改成功!"
    48. });
    49. // 路由跳转
    50. this.$router.push({ path: "/teacher/list" });
    51. });
    52. }
    53. }
    54. };
    55. script>

     对上面的添加和修改模块进行说明:因为添加和修改模块使用的是同一个组件add.vue。但是二者在使用该组件时使用方法不同。这个页面有一个保存按钮。当进行新增讲师的时候,进入该组件时表单里面的各项都是空的,再填写完成表单之后,点击保存按钮,会触发保存saveOrUpdate()的方法,但是执行该方法不能确定是进行新增操作还是进行修改操作。在讲师管理页面中有一个修改的按钮,点击后会进行路由跳转操作,并且这个路由跳转需要携带这一行记录的id。再点击修改按钮后会携带需要修改的这条记录的id然后跳转到新增页面并进行数据回显。要想完成此操作必须要在router中增加携带参数的路由

    1. {
    2. path: 'add',
    3. name: '添加讲师',
    4. component: () => import('@/views/teacher/add'),
    5. meta: { title: '添加讲师', icon: 'tree' }
    6. },
    7. {
    8. path: 'add/:id',
    9. name: 'EduTeacherEdit',
    10. component: () => import('@/views/teacher/add'),
    11. meta: { title: '编辑讲师', noCache: true },
    12. hidden: true
    13. }

     这两个路由是路由到同一个页面,但是功能不同,一个是带参数一个不带参数。

     this.$router.push({ path: "/teacher/list" });

    上面是进行路由跳转的语句。新增和修改都会使用上面的语句进行路由跳转。但是在跳转的过程中修改是带参数id的,新增不带参数直接跳转到该页面。如果携带了参数id要调用后台根据id查询的方法进行数据回显。数据回显的时候通过判断this.$route.params是否为空以this.$route.params.id是否存在来判断。如果这两个值都存在,说明是进行修改操作,在这个页面一加载时就进行数据回显;如果这两个值不存在就进行的新增操作,页面加载时所有的表单项都是空的。这个页面保存按钮如何判断是新增还是修改操作。通过判断id是否存在,存在就是进行修改操作,通过js调用后台的修改接口;不存在就是进行新增操作,通过后台调用js的新增接口。保存完成后又会进行页面跳转,跳转到讲师列表

  • 相关阅读:
    SpringCloud电商项目开发完整流程
    剑指 Offer 04. 二维数组中的查找
    CodeBERT理解
    Docker原生网络、自定义网络、Docker容器通信、跨主机容器网络
    计算机的发展史,让你想到了什么?
    Eclipse如何搭建一个SpringBoot项目
    观察者模式在spring中的应用
    One Thread One Loop主从Reactor模型⾼并发服务器
    OpenCV图像处理方法:腐蚀操作
    CAD二次开发--点击窗体按钮后还要再次点击CAD获取焦点才能进行操作?【winform/wpf与CAD焦点切换滞后问题解决办法】
  • 原文地址:https://blog.csdn.net/kobe_IT/article/details/128214329