mode.hbs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <script lang="ts" setup>
  2. import DetailForm from '../DetailForm/index.vue'
  3. const props = defineProps({
  4. ...DetailForm.props,
  5. modelValue: {
  6. type: Boolean,
  7. default: false,
  8. },
  9. mode: {
  10. type: String,
  11. default: 'dialog',
  12. validator: (val: string) => ['dialog', 'drawer'].includes(val),
  13. },
  14. })
  15. const emit = defineEmits(['update:modelValue', 'success'])
  16. const formRef = ref()
  17. const myVisible = computed({
  18. get() {
  19. return props.modelValue
  20. },
  21. set(val) {
  22. emit('update:modelValue', val)
  23. },
  24. })
  25. const title = computed(() => props.id === '' ? '新增{{ cname }}' : '编辑{{ cname }}')
  26. function onSubmit() {
  27. // submit() 为组件内部方法
  28. formRef.value.submit().then(() => {
  29. emit('success')
  30. onCancel()
  31. })
  32. }
  33. function onCancel() {
  34. myVisible.value = false
  35. }
  36. </script>
  37. <template>
  38. <div>
  39. {{!-- 为什么需要在 dialog 关闭的时候销毁 dialog 中的元素?因为该 dialog 为详情表单,新增的时候会用到,编辑的时候也会用到,不销毁则需要增加代码做处理,例如需要监听当传入的 id,当变 id 更后,需要重新执行生命周期进行表单初始化,同时关闭 dialog 时还需要清空表单,避免出现再次打开 dialog 时表单还保留上一次的信息数据,当表单复杂度上去后,维护这块的成本过高,drawer 同理 --}}
  40. <el-dialog v-if="props.mode === 'dialog'" v-model="myVisible" :title="title" width="600px" :close-on-click-modal="false" append-to-body destroy-on-close>
  41. <DetailForm ref="formRef" v-bind="props" />
  42. <template #footer>
  43. <el-button size="large" @click="onCancel">
  44. 取消
  45. </el-button>
  46. <el-button type="primary" size="large" @click="onSubmit">
  47. 确定
  48. </el-button>
  49. </template>
  50. </el-dialog>
  51. <el-drawer v-else-if="props.mode === 'drawer'" v-model="myVisible" :title="title" size="600px" :close-on-click-modal="false" destroy-on-close>
  52. <DetailForm ref="formRef" v-bind="props" />
  53. <template #footer>
  54. <el-button size="large" @click="onCancel">
  55. 取消
  56. </el-button>
  57. <el-button type="primary" size="large" @click="onSubmit">
  58. 确定
  59. </el-button>
  60. </template>
  61. </el-drawer>
  62. </div>
  63. </template>