Home > Web Front-end > Vue.js > How to use element-plus's dialog in vue3

How to use element-plus's dialog in vue3

WBOY
Release: 2023-05-11 21:13:13
forward
2261 people have browsed it

Advantages

Get rid of the cumbersome naming of visible and repeated dom.

Idea

Encapsulate the dialog into a component that can be evoked by a function. As follows:

addDialog({
  title: "测试", //弹窗名
  component: TestVue, //组件
  width: "400px", //弹窗大小
  props: {
    //传给组件的参数
    id: 0
  },
  callBack: (data: any) => {
    //当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面)
    console.log("回调函数", data)
  }
})
Copy after login

Based on el-dialog for preliminary encapsulation

// 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)
}
Copy after login
<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>
Copy after login
  • First define the dialogList, which contains the information of all pop-up windows.

  • component Use component is to dynamically load sub-components

  • addDialog calls the function that evokes the pop-up window

  • closeDialog function to close the pop-up window

Mounted in app.vue

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

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

<style scoped>

</style>
Copy after login

Use

to create a pop-up window component

<!-- test.vue -->
<template>
  父弹窗
  <el-button type="primary" @click="openChildDialog">打开子dialog</el-button>
  <el-button type="primary" @click="closeDialog">关闭弹窗</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>
Copy after login

Wake up the pop-up window on the list page

<!-- 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) => {
      //当弹窗任务结束后,调用父页面的回掉函数。(比如我新增完成了需要刷新列表页面)
      console.log("回调函数", data)
    }
  })
}
Copy after login

Multi-level pop-up window nesting

<!-- child.vue -->
<template>
  子弹窗
  <el-button type="primary" @click="closeDialog">关闭弹窗</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>
Copy after login

The above is the detailed content of How to use element-plus's dialog in vue3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template