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

如何通過Vue3+Element?Plus自定義彈出框組件

 更新時(shí)間:2024年05月11日 10:24:31   作者:擺脫小白  
這篇文章主要給大家介紹了關(guān)于如何通過Vue3+Element?Plus自定義彈出框組件的相關(guān)資料,彈窗是前端開發(fā)中的一種常見需求,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

在Vue 3項(xiàng)目中,使用Element Plus構(gòu)建彈出框是一項(xiàng)常見的任務(wù)。為了簡化這個(gè)過程,我們可以封裝一個(gè)公共組件,使彈出框的調(diào)用變得簡單而高效。本文將介紹如何通過Vue 3和Element Plus,使用一個(gè)自定義的彈出框組件實(shí)現(xiàn)這一目標(biāo)。

1. 彈出框組件封裝

首先,我們封裝了一個(gè)通用的彈出框組件,具體實(shí)現(xiàn)位于 util.js 文件中。通過 initInstance 方法,我們可以動(dòng)態(tài)創(chuàng)建一個(gè)彈出框?qū)嵗?,并將其掛載到指定的容器上。以下是簡要代碼:

// util.js

import { h, render } from 'vue';

export function initInstance(component, container, option) {
    const vNode = h(component, option);
    render(vNode, container);
    document.body.appendChild(container.firstElementChild);
    return vNode.component;
}

export function getContainer() {
    return document.createElement('div');
}

2. 自定義彈出框配置

接下來,我們定義了一個(gè)名為 portPop 的自定義彈出框配置。這個(gè)配置使用了前面封裝的通用方法,同時(shí)為彈出框提供了一些特定的配置。以下是簡要代碼:

// common.js

import { initInstance, getContainer } from "./util";
import PortPop from "../components/PortPop";

const instanceMap = new Map();

export const portPop = (option, call) => {
    const container = getContainer();
    let opt = {
        ...option,
        onComfrim: (data) => {
            call(data);
        },
        onVanish: () => {
            render(null, container);
            instanceMap.delete(vm);
        },
    };
    const component = initInstance(PortPop, container, opt);
    const vm = component.proxy;
    component.exposed.openDialog();
    instanceMap.set(vm, { option: opt });
};

3. 彈出框組件實(shí)現(xiàn)

首先,我們要先封裝一下el-dialog組件 

<template>
  <el-dialog
    :model-value="modelValue"
    :title="title"
    :width="width"
    :modal="modal"
    draggable
    destroy-on-close
    :close-on-click-modal="false"
    append-to-body
    :before-close="beforeClose"
    @close="dialogColse"
    @closed="dialogColsed"
  >
    <slot />
    <template #footer>
      <span class="dialog-footer">
        <slot name="footer" />
      </span>
    </template>
  </el-dialog>
</template>
<script setup name="Dialog">
import { ElDialog } from "element-plus";
const props = defineProps({
  modelValue: {
    type: [Boolean],
    default: false,
  },
  title: {
    type: [String],
    default: "",
  },
  width: {
    type: [String],
    default: "",
  },
  modal: {
    type: [Boolean],
    default: false,
  },
  beforeClose: [Function],
});
const emits = defineEmits(["update:modelValue", "onClosed", "closed"]);
function dialogColse() {
  emits("update:modelValue", false);
}
function dialogColsed() {
  emits("onClosed", false);
  emits("closed", false);
}
</script>

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

最后,我們實(shí)現(xiàn)了具體的彈出框組件 PortPop,這個(gè)組件使用了 Element Plus 的 Dialog 組件,并在其中嵌套了一些其他組件,以實(shí)現(xiàn)特定的功能。以下是簡要代碼:

<template>
  <div>
    <Dialog :title="props.title" v-model="dialog" width="920px">
        <!-- ... 彈出框內(nèi)容 ... -->
        <template #footer>
            <div class="dialog-footer">
              <el-button type="primary" size="small" @click="confrim">
                確定
              </el-button>
              <el-button @click="dialog = false" size="small">取消</el-button>
            </div>
          </template>
    </Dialog>
  </div>
</template>

<script setup lang="tsx">
import {
  nextTick,
  ref, useAttrs,
} from 'vue'
import Dialog from '@/components/dialog'
import {
  ElInput,
  ElButton,
} from 'element-plus'

const props = defineProps({
  title: {
    type: [String],
    default: '',
  },
  type: {
    type: [String],
    default: '',
  },
})

const emits = defineEmits(['comfrim'])
const attrs = useAttrs()
const dialog = ref(false)
function openDialog() {
  dialog.value = true
}
const confrim = async () => {
  emits('comfrim', "傳遞給父組件的數(shù)據(jù)")
  nextTick(() => {
    dialog.value = false
  })
}
defineExpose({ openDialog })

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

4. 使用自定義彈出框組件

最終,我們可以在需要調(diào)用彈出框的地方,簡單地執(zhí)行portPop方法,并傳遞相應(yīng)的配置和回調(diào)函數(shù)。這樣,彈出框就會(huì)顯示出來,用戶可以與之交互。

// 在需要的地方調(diào)用自定義彈出框
import { portPop } from "./customDialog";

// 示例調(diào)用
portPop({ title: '自定義彈出框', defaultKeyword: '關(guān)鍵字' }, (data) => {
    // 處理彈出框確認(rèn)后的數(shù)據(jù)
    console.log('用戶選擇的數(shù)據(jù):', data);
});

文件結(jié)構(gòu)示例

組定義組件文件示例

效果:

總結(jié) 

到此這篇關(guān)于如何通過Vue3+Element Plus自定義彈出框組件的文章就介紹到這了,更多相關(guān)Vue3 ElementPlus自定義彈出框組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決

    vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決

    這篇文章主要介紹了vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • webpack搭建vue環(huán)境時(shí)報(bào)錯(cuò)異常解決

    webpack搭建vue環(huán)境時(shí)報(bào)錯(cuò)異常解決

    這篇文章主要介紹了webpack搭建vue環(huán)境時(shí)報(bào)錯(cuò)異常解決,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • vue封裝form表單組件拒絕重復(fù)寫form表單

    vue封裝form表單組件拒絕重復(fù)寫form表單

    這篇文章主要為大家介紹了vue封裝form表單組件拒絕重復(fù)寫form表單的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法

    vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法

    這篇文章主要介紹了vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue打包添加gzip配置項(xiàng)方式

    vue打包添加gzip配置項(xiàng)方式

    這篇文章主要介紹了vue打包添加gzip配置項(xiàng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue初始化中的選項(xiàng)合并之initInternalComponent詳解

    Vue初始化中的選項(xiàng)合并之initInternalComponent詳解

    這篇文章主要介紹了Vue初始化中的選項(xiàng)合并之initInternalComponent的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • mint-ui在vue中的使用示例

    mint-ui在vue中的使用示例

    Mint UI 是 由餓了么前端團(tuán)隊(duì)推出的 一個(gè)基于 Vue.js 的移動(dòng)端組件庫,這篇文章主要介紹了mint-ui在vue中的使用示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下
    2018-04-04
  • vue element-ui中table合計(jì)指定列求和實(shí)例

    vue element-ui中table合計(jì)指定列求和實(shí)例

    這篇文章主要介紹了vue element-ui中table合計(jì)指定列求和實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 第一次接觸神奇的前端框架vue.js

    第一次接觸神奇的前端框架vue.js

    第一次接觸神奇的vue.js,主要了解一下v-model、v-if、v-else、v-show、v-for等,感興趣的小伙伴們可以一起學(xué)習(xí)一下
    2016-12-12
  • vue中多附件上傳的實(shí)現(xiàn)示例

    vue中多附件上傳的實(shí)現(xiàn)示例

    這篇文章主要介紹了vue中多附件上傳的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論