vue3 easyui dialog没有属性控制显隐问题

发布时间 2024-01-07 21:32:23作者: 邪妖怪
2024年1月7日21:11:37

vue3 v3-easyui中dialog没有属性控制显隐,通过观察源码得知dialog.closedState属性可以控制

v3-easyui源码如下:

图1

个人封装代码如下:

/** modal.vue */
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import type { CSSProperties } from 'vue'

const emits = defineEmits(['confirm'])

type Prop = {
  title?: string
  dialogStyle?: CSSProperties
  modal?: boolean
  closable?: boolean
  draggable?: boolean
  resizable?: boolean
  border?: boolean
  bottonGroup?: boolean
}

const props = withDefaults(defineProps<Prop>(), {
  title: '',
  dialogStyle: () => ({ width: '20%', height: '20%' }),
  modal: false,
  closable: true,
  draggable: false,
  resizable: false,
  border: true,
  bottonGroup: false
})
const dialogRef = ref<any>()

onMounted(() => {
  dialogRef.value.closedState = true
})

function confirm() {
  emits('confirm', '确认')
}

function cancel() {
  visible(false)
}

function visible(value: boolean) {
  dialogRef.value.closedState = !value
}

defineExpose({
  visible
})
</script>

<template>
  <Dialog
    ref="dialogRef"
    :title="title"
    :dialogStyle="dialogStyle"
    :modal="modal"
    :closable="closable"
    :draggable="draggable"
    :resizable="resizable"
    :closedState="false"
    buttons="#bb"
  >
    <div class="w-full h-full flex flex-col">
      <div class="break-all whitespace-normal overflow-hidden">
        <slot name="header"></slot>
      </div>
      <div class="flex-1 break-all whitespace-normal overflow-hidden">
        <!-- 滚动插件vue3-perfect-scrollbar -->
        <perfect-scrollbar class="h-full">
          <slot name="default"></slot>
        </perfect-scrollbar>
      </div>
      <div class="mb-4 flex justify-end" v-if="bottonGroup">
        <LinkButton
          class="min-w-20 !mr-4"
          iconCls="icon-ok"
          text="确定"
          size="small"
          @click="confirm"
        />
        <LinkButton
          class="min-w-20 !mr-4"
          iconCls="icon-cancel"
          text="取消"
          size="small"
          @click="cancel"
        />
      </div>
    </div>
  </Dialog>
</template>

<style lang="scss" scoped></style>

使用形式:

/** 父组件template */
<template>
  ......
  <button @click="handleClick">click</button>
  <Modal
    title="用户信息"
      :modal="true"
      :bottonGroup="true"
      @confirm="modalMsg"
      ref="modalRef"
    >
    <template #header> head </template>
    <template #default> </template>
  </Modal>
  ......
</template>

<script lang='ts' setup>
import { ref } from 'vue';
import Modal from '@/components/modal.vue'

const modalRef = ref<InstanceType<typeof Modal> | null>(null)

function handleClick() {
  modalRef.value?.visible(true)
}

function modalMsg(e) {
  console.log(e)
  modalRef.value?.visible(false)
}
</script>

图2