最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3全局掛載Dialog組件的示例代碼

 更新時間:2024年12月23日 11:25:34   作者:雨菁ling  
Dialog通常是指在Vue.js 3.x版本中使用的對話框組件,它是一個輕量級、易集成的彈窗插件,用于創(chuàng)建通知、確認(rèn)消息、輸入表單等交互場景,最近項目中遇到了全局掛載Dialog的需求,所以本文給大家介紹了Vue3全局掛載Dialog組件的方法,需要的朋友可以參考下

一、背景描述

最近項目中遇到了全局掛載Dialog的需求,在這里記錄一下,希望可以幫到大家。

二、Dialog組件封裝

2.1 Dialog.vue

<template>
  <!-- 刪除文件對話框 -->
  <el-dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    :width="dialogWidth"
		:before-close="handleBeforeClose"
  >
    <div>{{ title }}</div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleDialogClose">取 消</el-button>
        <el-button
          type="primary"
          :loading="sureBtnLoading"
          @click="handleDialogSure"
          >確 定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {
  ref,
  computed
} from 'vue'
import {
  deleteFile
} from '@/api/file'
import { ElMessage } from 'element-plus';

const props = defineProps({
  fileInfo: {
    type: Array,
    default: [],
  },
  title: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "550px",
  },
  saveClick: {
    type: Function,
    default: () => {},
  },
  cancelClick: {
    type: Function,
    default: () => {},
  },
  remove: {
    type: Function,
    default: () => {},
  }
})

const dialogVisible = ref(false) // 窗體顯示控制
const dialogWidth = computed(() => {
  return props.width || "550px"
})
const dialogTitle = computed(() => {
  return props.title || '刪除文件'
})

const title = computed(() => {
  return '此操作將永久刪除, 是否繼續(xù)?'
})

const sureBtnLoading = ref(false)
// 關(guān)閉回調(diào)觸發(fā)事件
const handleBeforeClose = (done) => {
  done()
}
// 取消按鈕點擊事件
const handleDialogClose = () => {
  props.cancelClick()
  props.remove()
}
// 確定按鈕點擊事件
const handleDialogSure = () => {
	sureBtnLoading.value = true
  deleteFile({
    id: props.fileInfo.id
  })
    .then((res) => {
      sureBtnLoading.value = false
      ElMessage.success(res.data || '刪除成功')
      props.saveClick()
      props.remove()
    })
    .catch(() => {
      sureBtnLoading.value = false
    })
}
</script>

2.2 index.js

import DeleteFileDialog from './Dialog.vue'
import { createApp } from 'vue'

let mountNode
let createMount = (opts) => {
	if(mountNode){ // 確保只存在一個彈框
		document.body.removeChild(mountNode)
    mountNode = null
	}
  mountNode = document.createElement('div')
  document.body.appendChild(mountNode)
  const app = createApp(DeleteFileDialog, {
    ...opts,
    modelValue: true,
    remove() { // 傳入remove函數(shù),組件內(nèi)可移除dom 組件內(nèi)通過props接收
      app.unmount(mountNode)
      document.body.removeChild(mountNode)
      mountNode = null
    }
  })
  return app.mount(mountNode)
}
function DeleteFile(options = {}) {
  options.id = options.id || 'deleteFile_' + 1 //唯一id 刪除組件時用于定位
  let $init = createMount(options)
  return $init
}
DeleteFile.install = app => {
  app.component('delete-file-dialog', DeleteFileDialog)
  app.config.globalProperties.$deleteFileDialog = DeleteFile
}
export default DeleteFile

三、全局掛載Dialog組件

在main.js中進行全局掛載

import DeleteFileDialog from '@/components/dialog/deleteFile/index.js'
app.use(DeleteFileDialog)

四、使用Dialog組件

proxy.$deleteFileDialog({
    fileInfo: fileInfo,
    saveClick: () => {
		console.log('saveClick---')
	},
	cancelClick: () => {
		console.log('cancelClick---')
	}
})

五、效果圖

到此這篇關(guān)于Vue3全局掛載Dialog組件的示例代碼的文章就介紹到這了,更多相關(guān)Vue3全局掛載Dialog內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

阿拉善盟| 北票市| 绥棱县| 周至县| 习水县| 五原县| 扎鲁特旗| 吉安县| 广宁县| 保靖县| 弋阳县| 许昌县| 珲春市| 新巴尔虎右旗| 达孜县| 澄城县| 白玉县| 唐山市| 汕头市| 莎车县| 普宁市| 综艺| 兴宁市| 桃园县| 通许县| 延安市| 司法| 临澧县| 文昌市| 梁河县| 双辽市| 阆中市| 新竹市| 永吉县| 中西区| 云霄县| 普兰店市| 休宁县| 托克托县| 道孚县| 都匀市|