欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡單三步在vue3中實現(xiàn)全局js調(diào)用彈窗組件(親測可用)

 更新時間:2025年08月14日 08:27:01   作者:GD-Frontender  
這篇文章主要介紹了在vue3中實現(xiàn)全局js調(diào)用彈窗組件的相關(guān)資料,文中通過創(chuàng)建Dialog.vue組件、index.js導(dǎo)出及main.js注冊,實現(xiàn)JS調(diào)用彈窗功能,需要的朋友可以參考下

前言

vue2中我們使用extend方式可以封裝一個全局js方式調(diào)用的彈窗組件

但是vue3中已經(jīng)摒棄了extend方法,那如何在vue3的架構(gòu)中去封裝這樣的組件呢?

第一步編寫組件模板

在components目錄下新建文件夾dialog,在該目錄下新建Dialog.vue和index.js文件

Dialog.vue文件:

<template>
    <div class="self-dialog">
        <el-dialog
            v-model="dialogData.show"
            :title="dialogData.title"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            destroy-on-close
            :width="dialogData.width"
            :before-close="handleClose"
        >
            <span>{{ dialogData.content }}</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="handleClose">取消</el-button>
                    <el-button type="primary" @click="handleConfirm">確定</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { inject, getCurrentInstance, computed } from 'vue'
const config = inject('config')
const { proxy } = getCurrentInstance()
const dialogData = computed(() => {
    let defaultData = {
        show: false,
        content: '',
        width: '800px',
        title: '彈窗',
        onConfirm: () => {},
        onCancel: () => {}
    }
    return {
        ...defaultData,
        ...config
    }
})
const handleClose = () => {
    dialogData.value.onCancel()
    document.body.removeChild(proxy.$el.parentNode)
}

const handleConfirm = () => {
    dialogData.value.onConfirm()
    document.body.removeChild(proxy.$el.parentNode)
}
</script>

index.js文件:

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

class Dialog {
    constructor() {
        this.instance = null
        this.mountDom = document.createElement('div')
    }
    show(options) {
        this.instance && this.instance.unmount()
        this.instance = createApp(dialog)
        this.instance.provide('config', options)
        this.instance.mount(this.mountDom)
        document.body.appendChild(this.mountDom)
    }
    hide() {
        this.instance && this.instance.unmount()
    }
}

export default new Dialog()

main.js文件:

// js調(diào)用全局彈窗
import Dialog from './components/dialog/index.js'
app.config.globalProperties.$dialog = Dialog

接下來就可以在vue組件中通過js方式調(diào)用了?。。。?/p>

總結(jié)

到此這篇關(guān)于在vue3中實現(xiàn)全局js調(diào)用彈窗組件的文章就介紹到這了,更多相關(guān)vue3全局js調(diào)用彈窗組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vuecli4配置sass與less詳解

    vuecli4配置sass與less詳解

    這篇文章主要為大家介紹了vuecli4配置sass與less詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的示例代碼

    Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的示例代碼

    這篇文章主要詳細(xì)介紹了Vue3使用ref與reactive創(chuàng)建響應(yīng)式對象的方法步驟,文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下
    2024-02-02
  • Vuejs第十篇之vuejs父子組件通信

    Vuejs第十篇之vuejs父子組件通信

    這篇文章主要介紹了Vuejs第十篇之vuejs父子組件通信的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 在Vue當(dāng)中同時配置多個路由文件的方法案例代碼

    在Vue當(dāng)中同時配置多個路由文件的方法案例代碼

    這篇文章主要介紹了在Vue當(dāng)中同時配置多個路由文件的方法,包含具體代碼,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • 通過html文件來使用Vue的單文件組件形式詳解

    通過html文件來使用Vue的單文件組件形式詳解

    這篇文章主要介紹了通過html文件來使用Vue的單文件組件形式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • vue自定義鍵盤實現(xiàn)車牌號的示例代碼

    vue自定義鍵盤實現(xiàn)車牌號的示例代碼

    本文主要介紹了vue自定義鍵盤實現(xiàn)車牌號的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Vue編譯器optimize源碼分析

    Vue編譯器optimize源碼分析

    這篇文章主要為大家介紹了Vue?編譯器optimize源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue組件詳解之使用slot分發(fā)內(nèi)容

    vue組件詳解之使用slot分發(fā)內(nèi)容

    這篇文章主要介紹了vue組件詳解之使用slot分發(fā)內(nèi)容及Vue組件中slot的用法,需要的朋友可以參考下
    2018-04-04
  • Vue前端整合Element?Ui的教程詳解

    Vue前端整合Element?Ui的教程詳解

    這篇文章主要介紹了Vue前端整合Element?Ui,本節(jié)內(nèi)容服務(wù)于SpringBoot?+?Vue?搭建?JavaWeb?增刪改查項目,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • mpvue 單文件頁面配置詳解

    mpvue 單文件頁面配置詳解

    這篇文章主要介紹了mpvue 單文件頁面配置詳解,本文將介紹如何在 mpvue 官方模板的基礎(chǔ)上,通過配置 mpvue-config-loader 來實現(xiàn)在 vue 文件內(nèi)書寫小程序的頁面配置,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論