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

vue3如何使用element-plus的dialog

 更新時間:2023年04月23日 09:30:51   作者:Yoge lv-3  
這篇文章主要介紹了vue3優(yōu)雅的使用element-plus的dialog,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

如何優(yōu)雅的基于 element-plus,封裝一個夢中情 dialog

優(yōu)點

擺脫繁瑣的 visible 的命名,以及反復(fù)的重復(fù) dom。

想法

將 dialog 封裝成一個函數(shù)就能喚起的組件。如下:

addDialog({
  title: "測試", //彈窗名
  component: TestVue, //組件
  width: "400px", //彈窗大小
  props: {
    //傳給組件的參數(shù)
    id: 0
  },
  callBack: (data: any) => {
    //當(dāng)彈窗任務(wù)結(jié)束后,調(diào)用父頁面的回掉函數(shù)。(比如我新增完成了需要刷新列表頁面)
    console.log("回調(diào)函數(shù)", data)
  }
})

效果圖

dialog.gif

基于 el-dialog 進行初步封裝

// index.ts
import { reactive } from "vue"
type dialogOptions = {
  title: string
  component: any
  props?: Object
  width: string
  visible?: any
  callBack?: Function
}
export const dialogList: dialogOptions[] = reactive([])

export const addDialog = (options: dialogOptions) => {
  dialogList.push(Object.assign(options, { visible: true }))
}

export const closeDialog = (item: dialogOptions, i: number, args: any) => {
  dialogList.splice(i, 1)
  item.callBack && item.callBack(...args)
}
<template>
  <Teleport to="body">
    <el-dialog
      v-for="(item, index) in dialogList"
      :key="index"
      :title="item.title"
      :width="item.width"
      v-model="item.visible"
    >
      <component :is="item.component" v-bind="item.props" @close="(...args:any) => closeDialog(item, index, args)" />
    </el-dialog>
  </Teleport>
</template>

<script setup lang="ts">
  import { dialogList, closeDialog } from "./index"
</script>
  • 首先定義了 dialogList,它包含了所有彈窗的信息。
  • component 使用 componet is 去動態(tài)加載子組件
  • addDialog 調(diào)用喚起彈窗的函數(shù)
  • closeDialog 關(guān)閉彈窗的函數(shù)

在app.vue中掛載

<script setup>
import Mydialog from "@/components/gDialog/index.vue"
</script>

<template>
 <router-view />
 <Mydialog></Mydialog>
</template>

<style scoped>

</style>

使用

創(chuàng)建一個彈窗組件

<!-- test.vue -->
<template>
  父彈窗
  <el-button type="primary" @click="openChildDialog">打開子dialog</el-button>
  <el-button type="primary" @click="closeDialog">關(guān)閉彈窗</el-button>
</template>

<script setup lang="ts">
  import { addDialog } from "@/components/gDialog/index"
  import childVue from "./child.vue"
  const props = defineProps(["id"])
  console.log(props.id, "props")
  const emit = defineEmits(["close"])
  const closeDialog = () => {
    emit("close", 1, 2, 34)
  }
  const openChildDialog = () => {
    addDialog({
      title: "我是子dialog",
      width: "500px",
      component: childVue
    })
  }
</script>

在列表頁面喚醒彈窗

<!-- list.vue -->
<template>
  列表頁
  <el-button type="primary" @click="openDialog">打開dialog</el-button>
</template>
<script setup lang="ts">
import { addDialog } from "@/components/gDialog/index"
import TestDialog from "./test.vue"
const openDialog = () => {
  addDialog({
    title: "我是dialog",
    width: "500px",
    props:{
      id:0
    }
    component: TestDialog,
    callBack: (data: any) => {
      //當(dāng)彈窗任務(wù)結(jié)束后,調(diào)用父頁面的回掉函數(shù)。(比如我新增完成了需要刷新列表頁面)
      console.log("回調(diào)函數(shù)", data)
    }
  })
}

多層級彈窗嵌套

<!-- child.vue -->
<template>
  子彈窗
  <el-button type="primary" @click="closeDialog">關(guān)閉彈窗</el-button>
</template>

<script setup lang="ts">
  import { addDialog } from "@/components/gDialog/index"
  const emit = defineEmits(["close"])
  const closeDialog = () => {
    emit("close", 1, 2, 34)
  }
</script>

附上代碼

代碼

到此這篇關(guān)于vue3優(yōu)雅的使用element-plus的dialog的文章就介紹到這了,更多相關(guān)vue3使用element-plus的dialog內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用Vue3實現(xiàn)文章內(nèi)容中多個"關(guān)鍵詞"標(biāo)記高亮顯示

    如何使用Vue3實現(xiàn)文章內(nèi)容中多個"關(guān)鍵詞"標(biāo)記高亮顯示

    高亮顯示是我們?nèi)粘i_發(fā)中經(jīng)常會遇到的需求,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3實現(xiàn)文章內(nèi)容中多個"關(guān)鍵詞"標(biāo)記高亮顯示的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽Enter鍵的方法與區(qū)別的相關(guān)資料,在Vue中我們可以通過監(jiān)聽鍵盤事件來實現(xiàn)回車鍵切換焦點的功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 詳解在vue3中使用jsx的配置以及一些小問題

    詳解在vue3中使用jsx的配置以及一些小問題

    本文主要介紹了在vue3中使用jsx的配置以及一些小問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue-router的HTML5 History 模式設(shè)置

    vue-router的HTML5 History 模式設(shè)置

    這篇文章主要介紹了vue-router的HTML5 History模式設(shè)置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Vue 引入AMap高德地圖的實現(xiàn)代碼

    Vue 引入AMap高德地圖的實現(xiàn)代碼

    這篇文章主要介紹了Vue 引入AMap高德地圖的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解vue組件開發(fā)腳手架

    詳解vue組件開發(fā)腳手架

    本篇文章給大家詳細(xì)分析了vue組件開發(fā)腳手架的相關(guān)內(nèi)容以及知識點,對此有興趣的朋友可以學(xué)習(xí)參考下。
    2018-06-06
  • Vue3+echarts5踩坑以及resize方法報錯的解決

    Vue3+echarts5踩坑以及resize方法報錯的解決

    這篇文章主要介紹了Vue3+echarts5踩坑以及resize方法報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue三層嵌套路由的示例代碼

    Vue三層嵌套路由的示例代碼

    本篇文章主要介紹了Vue三層嵌套路由的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Vue集成Iframe頁面的方法示例

    Vue集成Iframe頁面的方法示例

    這篇文章主要介紹了Vue集成Iframe頁面的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Vue組件實現(xiàn)旋轉(zhuǎn)木馬動畫

    Vue組件實現(xiàn)旋轉(zhuǎn)木馬動畫

    這篇文章主要為大家詳細(xì)介紹了Vue組件實現(xiàn)旋轉(zhuǎn)木馬動畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論