addGroupCom.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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="name">
  17. <el-input v-model="form.name" ></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. export default defineComponent({
  30. name: 'AddGroupCom',
  31. emits: ['closeDialog'],
  32. props: {
  33. flag: Boolean,
  34. dialogTitle: String,
  35. itemInfo: Object,
  36. },
  37. setup(props, context) {
  38. context
  39. const dialogVisible = ref(false)
  40. const formInfo = ref(null)
  41. const form = ref([])
  42. // open(): Dialog弹窗打开之前做的事
  43. const open = () => {
  44. form.value = props.itemInfo.value
  45. }
  46. // 关闭弹框
  47. const closeDialog = () => {
  48. context.emit('closeDialog', false)
  49. dialogVisible.value = false
  50. }
  51. watchEffect((fn, options) => {
  52. fn, options
  53. dialogVisible.value = props.flag
  54. })
  55. const roleValid = (rule, value, callback) => {
  56. rule
  57. if (value.length === 0) {
  58. callback(new Error('角色不能为空'))
  59. } else {
  60. callback()
  61. }
  62. }
  63. // 保存-修改操作
  64. const submitForm = () => {
  65. console.log(formInfo)
  66. formInfo.value.validate((valid) => {
  67. if (valid) {
  68. // 走保存请求
  69. this.$message({
  70. message: '操作成功!',
  71. type: 'success',
  72. })
  73. this.closeDialog(1)
  74. } else {
  75. console.log('error submit!!')
  76. return false
  77. }
  78. })
  79. }
  80. return {
  81. closeDialog,
  82. dialogVisible,
  83. roleValid,
  84. formInfo,
  85. form,
  86. open,
  87. submitForm,
  88. rules: {
  89. name: [
  90. // required 是否为必填项, trigger:表单验证的触发时机,失去焦点进行验证
  91. { required: true, message: "请输入分组名称", trigger: "blur" },
  92. {
  93. min: 3,
  94. max: 6,
  95. message: "用户名长度在 3 到 6 个字符",
  96. trigger: "blur",
  97. },
  98. ],
  99. },
  100. }
  101. },
  102. })
  103. </script>
  104. <style scoped lang="scss">
  105. </style>