vue3+ts+elementui-plus二次封裝彈框?qū)崙?zhàn)教程
vue3+ts+elementui-plus二次封裝彈框
一、彈框組件BaseDialog
<template>
<div class='main'>
<el-dialog v-model="visible" :title="title" :width="dialogWidth" :before-close="handleClose">
<!-- 內(nèi)容插槽 -->
<slot></slot>
<template #footer>
<span class="dialog-footer">
<el-button v-if="showCancelButton" @click="handleClose">取消</el-button>
<el-button v-if="showConfirmButton" type="primary" @click="handleConfirm">
確認(rèn)
</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script lang='ts' setup>
import { ElMessageBox } from 'element-plus'
import { ref, reactive, getCurrentInstance, onMounted, defineExpose, defineEmits } from 'vue'
/**
* 傳入的props變量
*/
const props = defineProps({
title: {
type: String,
default: '提示',
},
dialogWidth: {
type: String,
default: '40%',
},
showCancelButton: {
type: Boolean,
default: true,
},
showConfirmButton: {
type: Boolean,
default: true,
},
})
/**
* 發(fā)射給父組件的方法
* 用于子組件給父組件傳值或調(diào)用父組件方法
*/
const emits = defineEmits(['submit', 'close'])
const visible = ref(false)
// 關(guān)閉彈框
const handleClose = () => {
emits('close')
}
// 打開彈框
const handleOpen = () => {
visible.value = true
}
// 確認(rèn)事件
const handleConfirm = () => {
emits('submit')
}
/**
* 暴露給父組件的方法
* 用于父組件調(diào)用子組件方法或獲取子組件屬性值
*/
defineExpose({ handleOpen, handleClose, visible })
onMounted(() => {
})
</script>
<style scoped lang='scss'>
</style>二、在index.vue中使用
<el-button @click="showDialog">點(diǎn)擊出現(xiàn)彈框</el-button> <BaseDialog ref="baseDialog" @submit="handleSubmit" @close="handleClose"> <div> <el-input placeholder="Please input" /> </div> </BaseDialog>
<script lang='ts' setup>
import BaseDialog from '@/components/BaseDialog/index.vue'
import { ref, reactive, getCurrentInstance, onMounted } from 'vue'
onMounted(() => {
})
// 獲取子組件的ref
let baseDialog = ref()
// 點(diǎn)擊出現(xiàn)彈框
const showDialog = () => {
// 調(diào)用子組件方法,打開彈框
baseDialog.value.handleOpen()
}
// 彈框確認(rèn)事件
const handleSubmit = () => {
console.log('我是父組件中的確認(rèn)事件')
}
// 彈框取消事件
const handleClose = () => {
baseDialog.value.visible = false
}
</script>三、效果

創(chuàng)建vue3+ts+element-plus項(xiàng)目
一、創(chuàng)建vue3項(xiàng)目
1.新建vue3TsElement文件夾
該文件夾中打開命令行窗口,運(yùn)行創(chuàng)建vue3項(xiàng)目的命令
npm init vue@latest

2.打開創(chuàng)建好的vue3_TS_element文件夾
關(guān)掉之前的命令行窗口,在該文件夾中再次打開命令行窗口下載依賴文件
npm install

3.說明
注意創(chuàng)建項(xiàng)目命令行窗口與安裝依賴命令行窗口之間的切換
二、安裝element-plus組件庫
安裝命令
npm install element-plus --save
完整引入
在main.ts引入,就可以使用了
import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' app.use(ElementPlus)
按需引入
(1)首先要安裝element-plus
npm install element-plus --save
(2)然后你需要安裝unplugin-vue-components 和 unplugin-auto-import這兩款插件
npm install -D unplugin-vue-components unplugin-auto-import
(3)然后把下列代碼插入到你的 Vite 或 Webpack 的配置文件中
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
//按需引入element
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
AutoImport({//按需引入element
resolvers: [ElementPlusResolver()],
}),
Components({//按需引入element
resolvers: [ElementPlusResolver()],
}),
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})到此這篇關(guān)于vue3+ts+elementui-plus二次封裝彈框的文章就介紹到這了,更多相關(guān)vue3 ts elementui-plus彈框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3 update:modelValue用法小結(jié)
本文主要介紹了vue3 update:modelValue用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12
Vue頁面布局與路由映射實(shí)戰(zhàn)教程之RouterView嵌套及動(dòng)態(tài)組件生成詳解
文章介紹了布局組件在Vue應(yīng)用中的設(shè)計(jì)和使用,包括布局組件的核心作用、多層RouterView的嵌套布局、靜態(tài)組件與動(dòng)態(tài)組件的區(qū)別,以及動(dòng)態(tài)路由的優(yōu)勢(shì),通過這些機(jī)制,可以實(shí)現(xiàn)界面的一致性、靈活的權(quán)限控制和菜單擴(kuò)展,感興趣的朋友跟隨小編一起看看吧2026-01-01
詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法
這篇文章主要介紹了詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
基于Vue3和SpringBoot實(shí)現(xiàn)Web實(shí)時(shí)消息推送功能
這篇文章主要介紹了WebSocket實(shí)現(xiàn)實(shí)時(shí)通信,對(duì)比HTTP低效,通過SpringBoot+Vue整合實(shí)現(xiàn)消息推送,涵蓋聊天、股票等場(chǎng)景,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2025-05-05

