vue3+ts+element-plus實(shí)際開(kāi)發(fā)之統(tǒng)一調(diào)用彈窗封裝的詳細(xì)過(guò)程
插槽
1. 官網(wǎng)介紹
先理解 插槽、具名插槽、 作用域插槽、動(dòng)態(tài)插槽名、具名作用域插槽屬性和使用方法
2. 統(tǒng)一調(diào)用彈窗封裝dome實(shí)戰(zhàn)
- 使用場(chǎng)景:
大屏看板中,小模塊查看詳情信息功能
- 對(duì)el-dialog進(jìn)行數(shù)據(jù)動(dòng)態(tài)設(shè)置
新建一個(gè)one-dialog.vue文件,并修改成自己需要的組件。
<template>
<el-dialog
v-model="dialogTableVisible"
:title="title"
:width="width"
:top="top"
:custom-class="customClass"
:style="{ maxWidth: width, minWidth: width }"
destroy-on-close
align-center
append-to-body
>
<slot name="dialog" />
</el-dialog>
<!-- 定義一個(gè) @click 事件監(jiān)聽(tīng)器來(lái)綁定點(diǎn)擊事件到組件的 showDialog 方法上。 -->
<div style="cursor: pointer" @click="showDialog">
<!-- slot可以可以包裹在父組件要設(shè)置點(diǎn)擊事件的標(biāo)簽外層 ,來(lái)實(shí)現(xiàn)父組件內(nèi)調(diào)起彈窗-->
<slot />
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
defineProps({
title: String,
width: [String, Number],
customClass: String,
top: [String, Number],
});
const dialogTableVisible = ref(false);
const showDialog = () => {
dialogTableVisible.value = true;
};
</script>
<style scoped lang="scss">
</style>- 新建一個(gè)ts文件用于統(tǒng)一存放組件,類似下邊格式
export { default as Dialogone } from './one.vue';
export { default as Dialogtwo} from './two.vue';
export { default as DialogFancyButton} from './fancyButton.vue';
export { default as TableList} from '@/views/elementPlus/tableList.vue';- 封裝一個(gè)通用彈窗
新建組件one.vue,并且在one.vue里邊使用封裝好的one-dialog.vue組件
<template>
<!-- 彈窗 -->
<Dialogone title="表格詳情" width="700px" :dialogTableVisible="true">
<!-- 使用插槽向固定位置輸出內(nèi)容 #是v-slot簡(jiǎn)寫,這個(gè)SleFone要與父組件使用時(shí)候<template #SleFone>一致-->
<slot name="SleFone"> </slot>
<template #dialog>
<TableList v-if="type==='1'"></TableList>
<CarouselOne v-if="type==='2'"></CarouselOne>
</template>
</Dialogone>
</template>
<script setup lang="ts">
import { Dialogone } from "../../../components/index";
//這里我隨便拿了兩個(gè)頁(yè)面做組件使用,
import { TableList } from "../../../components/index";
import { CarouselOne } from "../../../components/index";
defineProps({
type: String,
});
</script>
<style scoped lang="scss">
</style>使用示例
我直接在表格詳情使用的,點(diǎn)擊詳情掉用組件



3. 多個(gè)頁(yè)面使用時(shí)候統(tǒng)一引用
新建一個(gè)GlobalComponents.ts文件
import { App } from 'vue';
import {SleFone} from './index';
// 創(chuàng)建一個(gè) install 函數(shù),接收 Vue 應(yīng)用實(shí)例作為參數(shù)
const install = (app: App) => {
// 在 Vue 應(yīng)用實(shí)例中注冊(cè) SleFone 組件
app.component('SleFone', SleFone);
// 在這里可以注冊(cè)其他全局組件
// app.component('AnotherComponent', AnotherComponent);
};
// 導(dǎo)出 install 函數(shù)
export default { install };在main.ts中統(tǒng)一引入
//自定義組件
import GlobalComponents from './components/GlobalComponents';
const app = createApp(App)
app.use(GlobalComponents);
app.mount('#app');頁(yè)面中不需要每個(gè)引用,可以直接使用
<SleFone type="1">
<template #SleFone>
//一下內(nèi)容可以自定義
<el-button
link
type="primary"
size="small"
@click="detailsClick(scope.row)"
>
點(diǎn)擊喚起彈窗
</el-button>
</template>
</SleFone>如果出現(xiàn)套盒子情況,2種處理方式
第一種處理方式
如果我們想在父組件沒(méi)有提供任何插槽內(nèi)容時(shí)在 內(nèi)渲染“Submit”,只需要將“Submit”寫在 標(biāo)簽之間來(lái)作為默認(rèn)內(nèi)容:
<button type="submit">
<slot name="SleFone2">
Submit <!-- 默認(rèn)內(nèi)容 -->
</slot>
</button>但如果我們提供了插槽內(nèi)容:=
那么被顯式提供的內(nèi)容會(huì)取代默認(rèn)內(nèi)容:
<template #SleFone2>
<span>新內(nèi)容</span>
</template>根據(jù)上邊插槽特性,反向使用


2. 第二種處理方式: 更換喚起彈窗的方式,根據(jù)實(shí)際情況也已使用全局變量控制
到此這篇關(guān)于vue3+ts+element-plus實(shí)際開(kāi)發(fā)之統(tǒng)一調(diào)用彈窗封裝的文章就介紹到這了,更多相關(guān)vue3 ts element-plus彈窗封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue開(kāi)發(fā)中關(guān)于axios的封裝過(guò)程
這篇文章主要介紹了vue開(kāi)發(fā)中關(guān)于axios的封裝過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
淺談Vue.js 關(guān)于頁(yè)面加載完成后執(zhí)行一個(gè)方法的問(wèn)題
這篇文章主要介紹了Vue.js 關(guān)于頁(yè)面加載完成后執(zhí)行一個(gè)方法的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解
這篇文章主要介紹了vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式
這篇文章主要介紹了vue3項(xiàng)目+element-plus:時(shí)間選擇器格式化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue-cli 3.0 引入mint-ui報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vue-cli 3.0 引入mint-ui報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令
本文主要介紹了vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
VUE渲染后端返回含有script標(biāo)簽的html字符串示例
今天小編就為大家分享 一篇VUE渲染后端返回含有script標(biāo)簽的html字符串示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

