addGroupCom.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <el-dialog
  3. :title="dialogTitle"
  4. v-model="dialogVisible"
  5. width="400px"
  6. @close="closeDialog(0)"
  7. @open="open"
  8. >
  9. <el-form
  10. ref="formInfo"
  11. :model="form"
  12. class="demo-form-inline"
  13. label-width="100px"
  14. :rules="rules"
  15. >
  16. <el-form-item label="分组名称:" prop="groupingName">
  17. <el-input v-model="form.groupingName" ></el-input>
  18. </el-form-item>
  19. <div style="text-align: right">
  20. <el-button type="primary" @click="submitForm('formInfo')"
  21. >保存</el-button
  22. >
  23. </div>
  24. </el-form>
  25. </el-dialog>
  26. </template>
  27. <script>
  28. import { defineComponent, ref, watchEffect } from 'vue'
  29. import * as api from '@/api/siteManage/index'
  30. import { ElMessage } from 'element-plus'
  31. export default defineComponent({
  32. name: 'AddGroupCom',
  33. emits: ['closeDialog'],
  34. props: {
  35. flag: Boolean,
  36. dialogTitle: String,
  37. itemInfo: Object,
  38. },
  39. setup(props, context) {
  40. context
  41. const dialogVisible = ref(false)
  42. const formInfo = ref(null)
  43. const form = ref([])
  44. // open(): Dialog弹窗打开之前做的事
  45. const open = () => {
  46. form.value = props.itemInfo.value
  47. }
  48. // 关闭弹框
  49. const closeDialog = () => {
  50. context.emit('closeDialog', false)
  51. dialogVisible.value = false
  52. }
  53. watchEffect((fn, options) => {
  54. fn, options
  55. dialogVisible.value = props.flag
  56. })
  57. const roleValid = (rule, value, callback) => {
  58. rule
  59. if (value.length === 0) {
  60. callback(new Error('角色不能为空'))
  61. } else {
  62. callback()
  63. }
  64. }
  65. // 保存-修改操作
  66. const submitForm = () => {
  67. console.log(formInfo)
  68. formInfo.value.validate((valid) => {
  69. if (valid) {
  70. api.siteGroupingAdd(form.value).then((requset) => {
  71. if (requset.status === 'SUCCESS') {
  72. ElMessage.success({
  73. message: '新增成功',
  74. type: 'success',
  75. })
  76. closeDialog()
  77. } else {
  78. ElMessage.error(requset.msg)
  79. }
  80. })
  81. } else {
  82. console.log('error submit!!')
  83. return false
  84. }
  85. })
  86. }
  87. return {
  88. closeDialog,
  89. dialogVisible,
  90. roleValid,
  91. formInfo,
  92. form,
  93. open,
  94. submitForm,
  95. rules: {
  96. groupingName: [
  97. // required 是否为必填项, trigger:表单验证的触发时机,失去焦点进行验证
  98. { required: true, message: "请输入分组名称", trigger: "blur" },
  99. {
  100. min: 3,
  101. max: 6,
  102. message: "用户名长度在 3 到 6 个字符",
  103. trigger: "blur",
  104. },
  105. ],
  106. },
  107. }
  108. },
  109. })
  110. </script>
  111. <style scoped lang="scss">
  112. </style>