1. <template>
  2. <div class="dashboard-container">
  3. <div class="app-container">
  4. <el-card>
  5. <!-- 新增角色按钮 -->
  6. <el-row style="height:60px">
  7. <el-button
  8. icon="el-icon-plus"
  9. size="small"
  10. type="primary"
  11. >新增角色</el-button>
  12. </el-row>
  13. <!-- 表格 -->
  14. <el-table>
  15. <el-table-column label="序号" width="120" />
  16. <el-table-column label="角色名称" width="240" />
  17. <el-table-column label="描述" />
  18. <el-table-column label="操作" width="240" fixed="right">
  19. <template>
  20. <el-button size="small">分配权限</el-button>
  21. <el-button size="small">编辑</el-button>
  22. <el-button size="small" type="danger">删除</el-button>
  23. </template>
  24. </el-table-column>
  25. </el-table>
  26. <!-- 分页组件 -->
  27. <el-row type="flex" justify="center" align="middle" style="height: 60px">
  28. <!-- 分页组件 -->
  29. <el-pagination layout="prev,pager,next" />
  30. </el-row>
  31. </el-card>
  32. </div>
  33. </div>
  34. </template>

数据获取

  1. /**
  2. * @description: 获取角色列表
  3. * @param {*} params:{page 页码, pagesize 每页的条数}
  4. * @return {*}
  5. */
  6. export function getRoleListApi(params) {
  7. return request({
  8. method: 'get',
  9. url: '/sys/role',
  10. params
  11. })
  12. }

el-table-column 中 prop属性绑定表格当前列渲染的数据 接口需要数据 先定义一个假数据 获取到真实数据在将数据更改

  1. export default {
  2. data() {
  3. return {
  4. total: 0,
  5. list: [],
  6. parmas: {
  7. page: 1, // 页码
  8. pagesize: 5 // 每页显示条数
  9. },
  10. }
  11. },
  12. mounted() {
  13. this.getRoleList() // 获取角色列表
  14. },
  15. methods: {
  16. // 获取角色列表
  17. async getRoleList() {
  18. const { data } = await getRoleListApi(this.parmas)
  19. // console.log(data)
  20. this.list = data.rows
  21. this.total = data.total
  22. }
  23. }
  24. }

添加分页器

page-size 绑定每页显示的条数 total 绑定数据总数 @current-change=”currentChange” 可以获取到当前页面的页数 根据当前页面页数 调用接口查看不同的数据

  1. <!-- 分页组件 -->
  2. <el-row type="flex" justify="center" align="middle" style="height: 60px">
  3. <!-- 分页组件 -->
  4. <el-pagination :page-size="parmas.pagesize" :total="total" layout="prev,pager,next" @current-change="currentChange" />
  5. </el-row>
  1. // 切换分页展示不同页数的数据
  2. currentChange(page) {
  3. // console.log(page)
  4. // 获取到当前点击的页数
  5. this.parmas.page = page
  6. // 重新获取列表
  7. this.getRoleList(this.parmas)
  8. },

增加角色

封装一个弹层模块

利用 父传子 子传父 来控制弹层的打开关闭 子传父的本质:子组件发送一个通知给父组件 调用父组件的一个方法 可以不用传参 父传子的注意点:数据类型必须匹配 以type为主

  1. <template>
  2. <!-- 新增弹框 -->
  3. <el-dialog :visible="showDialog" title="编辑弹层" @close="closeRoleDialog">
  4. <!-- 表单内容 -->
  5. <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
  6. <el-form-item label="角色名称" prop="name">
  7. <el-input v-model="roleForm.name" />
  8. </el-form-item>
  9. <el-form-item label="角色描述" prop="description">
  10. <el-input v-model="roleForm.description" />
  11. </el-form-item>
  12. </el-form>
  13. <!-- 底部 -->
  14. <el-row slot="footer" type="flex" justify="center">
  15. <el-button size="small" @click="cancel">取消</el-button>
  16. <el-button size="small" type="primary" @click="confirm">确定</el-button>
  17. </el-row>
  18. </el-dialog>
  19. </template>
  20. <script>
  21. import { addRoleApi } from '@/api/setting'
  22. export default {
  23. props: {
  24. showDialog: {
  25. type: Boolean
  26. }
  27. },
  28. data() {
  29. return {
  30. roleForm: {
  31. name: '',
  32. description: ''
  33. },
  34. rules: {
  35. name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
  36. description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
  37. }
  38. }
  39. },
  40. methods: {
  41. // 关闭弹层 (子传父)
  42. closeRoleDialog() {
  43. this.$emit('close_dialog')
  44. },
  45. // 点击确定触发
  46. confirm () {
  47. // 表单校验
  48. this.$refs.roleForm.validate(async isOk => {
  49. if (isOk) {
  50. try {
  51. // 提交数据
  52. await addRoleApi(this.roleForm)
  53. // 关闭弹层
  54. // this.closeRoleDialog()
  55. this.$emit('close_dialog')
  56. // 提示用户
  57. this.$message.success('新增角色成功')
  58. // 更新列表数据
  59. this.$emit('update_list')
  60. // 重置表单
  61. this.resteForm
  62. } catch (error) {
  63. this.$message.error(error)
  64. }
  65. }
  66. })
  67. },
  68. // 点击取消触发
  69. cancel() {
  70. this.resteForm()
  71. },
  72. // 重置表单
  73. resteForm() {
  74. this.closeRoleDialog()
  75. this.$refs.roleForm.resetFields()
  76. this.roleForm = {
  77. name: '',
  78. description: ''
  79. }
  80. }
  81. }
  82. }
  83. </script>
  84. <style>
  85. </style>
  1. <!-- 弹层组件 -->
  2. <AddDailong
  3. :show-dialog="isShow"
  4. @close_dialog="closeRoleDialog"
  5. @update_list="updateList"
  6. />
  7. <script>
  8. import AddDailong from './components/addDailog.vue'
  9. import { getRoleListApi } from '@/api/setting'
  10. export default {
  11. components: {
  12. AddDailong
  13. },
  14. data() {
  15. return {
  16. total: 0,
  17. list: [],
  18. parmas: {
  19. page: 1, // 页码
  20. pagesize: 5 // 每页显示条数
  21. },
  22. // 弹层组件的显隐
  23. isShow: false
  24. }
  25. },
  26. mounted() {
  27. this.getRoleList() // 获取角色列表
  28. },
  29. methods: {
  30. // 更新列表数据 子传父
  31. updateList() {
  32. this.getRoleList()
  33. },
  34. // 关闭弹层自定义事件
  35. closeRoleDialog () {
  36. this.isShow = false
  37. },
  38. // 获取角色列表
  39. async getRoleList() {
  40. const { data } = await getRoleListApi(this.parmas)
  41. // console.log(data)
  42. this.list = data.rows
  43. this.total = data.total
  44. },
  45. }
  46. }
  47. </script>
  48. </script>

删除功能

根据id删除角色 通过#default=”{row}”获取当前数据 传入row.id到删除函数内

  1. // 删除角色
  2. /**
  3. *
  4. * @param {*} id 当前行的id
  5. * @returns
  6. */
  7. export const delRoleApi = (id) => request.delete(`/sys/role/${id}`)
  1. // 删除功能
  2. delRole(id) {
  3. try {
  4. this.$confirm('确定要删除吗?', '提示', {
  5. confirmButtonText: '确定',
  6. cancelButtonText: '取消' }).then(() => {
  7. }).then(async () => {
  8. // 点击确定执行的代码
  9. await delRoleApi(id)
  10. this.$message.success('删除成功')
  11. // 重新获取列表
  12. this.getRoleList(this.parmas)
  13. }).catch(() => {
  14. // 点击取消执行的代码
  15. })
  16. } catch (error) {
  17. this.$message.error(error)
  18. }
  19. }

编辑功能

  1. // 根据id获取角色信息
  2. export const getRoleApi = (id) => request.get(`/sys/role/${id}`)
  3. // 根据id修改角色信息
  4. export const editRoleApi = (data) => request.put(`/sys/role/${data.id}`, data)

数据回填

将方法定义到弹层组件内 在父组件内用ref获取组件实例,因为方法是定义在组件内 可以通过组件实例直接调用

组件通信的弊端 和 ref1. 组件通信的麻烦
组件的抽离 必定会造成组件的嵌套 组件的嵌套必定会引起组件通信
所谓的组件通信其实就是大家理解的父子通信 兄弟通信 vuex
有一个要求 组件的抽象要保证 尽量少的组件通信 通信的次数越多 传递的数据越多 就越容易出错
2. ref
获取到组件实例 实例对象一旦被获取之后 它身上的属性和方法就都可以随便用
this.$refs.form.validate
尽量不要更改属性 你可以调用方法 用方法[methods]去修改属性[data] 不要直接修改
ref这种命令式的操作手段 可以在某些场景起到简化的作用 但是不建议大规模使用 破坏了数据流
可以少范围的使用减少组件通信成本

  1. // 编辑功能
  2. editRole(id) {
  3. // 打开弹层
  4. this.isShow = true
  5. // 数据回填 (获取组件实例 方法是定义在组件实例上的 直接调用组件上的方法 少用因为会破坏单项数据流)
  6. this.$refs.dialogRef.fetchDetail(id)
  7. }
  1. async fetchDetail(id) {
  2. const res = await getRoleApi(id)
  3. this.roleForm = res.data
  4. }

数据回填时获取到的数据里面多出来一些属性 这里使用id来判断 是否数据回填 是说明当前点击的是编辑按钮 否是提交

  1. // 点击确定触发
  2. confirm () {
  3. // 表单校验
  4. this.$refs.roleForm.validate(async isOk => {
  5. if (isOk) {
  6. try {
  7. // 提交数据
  8. if (this.roleForm.id) {
  9. await editRoleApi(this.roleForm)
  10. } else {
  11. await addRoleApi(this.roleForm)
  12. }
  13. // 关闭弹层
  14. // this.closeRoleDialog()
  15. this.$emit('close_dialog')
  16. // 提示用户
  17. this.$message.success(`${this.roleForm.id ? '修改' : '新增'}角色成功`)
  18. // 更新列表数据
  19. this.$emit('update_list')
  20. // 重置表单
  21. this.resteForm
  22. } catch (error) {
  23. this.$message.error(error)
  24. }
  25. }
  26. })
  27. },