Vue手寫(xiě)dialog組件模態(tài)框過(guò)程詳解
在vue項(xiàng)目下創(chuàng)建文件dialog

實(shí)現(xiàn)思路
1、dialog組件為模態(tài)框,因此應(yīng)該是固定定位到頁(yè)面上面的,并且需要留一定的插槽來(lái)讓使用者自定義顯示內(nèi)容
2、難點(diǎn)在于如何一句話打開(kāi)dialog,也就是下面的index.js文件的內(nèi)容:導(dǎo)入我們已經(jīng)寫(xiě)好的組件(可以先寫(xiě)一個(gè)及其簡(jiǎn)單的),模塊暴露出一個(gè)函數(shù)(DiaLog)用于生成dialog,這里主要利用到vue中的createApp函數(shù),createApp創(chuàng)建應(yīng)用,將應(yīng)用掛載到我們的新建div標(biāo)簽上,隨著用戶觸發(fā)點(diǎn)擊事件,將div標(biāo)簽銷毀即可
index.js文件
import dialog from './index.vue'
import { createApp} from 'vue'
export const DiaLog = (obj) => {
const app = createApp(dialog, {
...obj,
on_click: (flg) => {
console.log(flg);
div.remove()
},
})
const div = document.createElement('div')
app.mount(div)
document.body.appendChild(div)
}
使用
<template>
<div class="app">
<button @click="DiaLog({_title:'我不想起標(biāo)題'})">起飛</button>
</div>
</template>
<script setup>
import { DiaLog } from './package/dialog/index.js'
</script>
<style scoped lang="scss">
.app {
height: 1200px;
}
</style>
index.vue文件
<template>
<div class="dialog">
<h1 v-if="props._title">{{ props._title }}</h1>
<div>
<slot></slot>
</div>
<div class="btn">
<button @click="emitFn(false)">取消</button>
<button @click="emitFn(true)" class="success">確認(rèn)</button>
</div>
</div>
<div class="background" v-if="props._background"></div>
</template>
<script setup>
const props = defineProps({
_title: {
type: String,
default: '無(wú)標(biāo)題'
},
_background: {
type: Boolean,
default: true
}
})
const emit = defineEmits([
'_click'
])
const emitFn = (boolean) => {
emit('_click', boolean)
}
</script>
<style scoped lang="scss">
.dialog {
background-color: white;
z-index: 999;
position: fixed;
width: 400px;
min-height: 200px;
left: 50%;
top: 50%;
border: 1px solid rgba(0, 0, 0, 0.5);
transform: translateX(-50%) translateY(-50%);
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 15px;
h1 {
font-size: 20px;
font-weight: 400;
padding: 0;
margin: 0;
}
.btn {
display: flex;
justify-content: end;
button {
padding: 5px 15px;
border-radius: 5px;
border: 2px solid #E2E2E2;
font-size: 14px;
cursor: pointer;
}
.success {
color: white;
background-color: #36AD6A;
margin-left: 20px;
}
}
}
.background {
width: 100vw;
height: 100vh;
position: fixed;
left: 0;
// display: none;
top: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 99;
}
</style>
到此這篇關(guān)于Vue手寫(xiě)dialog組件模態(tài)框過(guò)程詳解的文章就介紹到這了,更多相關(guān)Vue dialog組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用mint-ui開(kāi)發(fā)項(xiàng)目的一些心得(分享)
下面小編就為大家?guī)?lái)一篇使用mint-ui開(kāi)發(fā)項(xiàng)目的一些心得(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
vue簡(jiǎn)單練習(xí) 桌面時(shí)鐘的實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了vue簡(jiǎn)單練習(xí) 桌面時(shí)鐘的實(shí)現(xiàn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值的相關(guān)資料,需要的朋友可以參考下2019-09-09
vue設(shè)置全局變量5種方法(讓你的數(shù)據(jù)無(wú)處不在)
這篇文章主要給大家介紹了關(guān)于vue設(shè)置全局變量的5種方法,通過(guò)設(shè)置的方法可以讓你的數(shù)據(jù)無(wú)處不在,在項(xiàng)目中經(jīng)常會(huì)復(fù)用一些變量和函數(shù),比如用戶的登錄token,用戶信息等,這時(shí)將它們?cè)O(shè)為全局的就顯得很重要了,需要的朋友可以參考下2023-11-11
Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)
最近在項(xiàng)目中需要對(duì)數(shù)據(jù)進(jìn)行可視化處理,而眾所周知echarts是非常強(qiáng)大的插件,下面這篇文章主要給大家介紹了關(guān)于Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)的相關(guān)資料,需要的朋友可以參考下2022-05-05
解決vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長(zhǎng)的問(wèn)題
vue-quill-editor默認(rèn)插入圖片是直接將圖片轉(zhuǎn)為base64再放入內(nèi)容中,如果圖片較多,篇幅太長(zhǎng),就會(huì)比較煩惱,接下來(lái)通過(guò)本文給大家介紹vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長(zhǎng)的問(wèn)題及解決方法,需要的朋友可以參考下2018-08-08
解決vue prop傳值default屬性如何使用,為何不生效的問(wèn)題
這篇文章主要介紹了解決vue prop傳值default屬性如何使用,為何不生效的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue中組件的過(guò)渡動(dòng)畫(huà)及實(shí)現(xiàn)代碼
這篇文章主要介紹了vue中組件的過(guò)渡動(dòng)畫(huà),并通過(guò)實(shí)例代碼給大家介紹了過(guò)渡動(dòng)畫(huà)的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
Vue.js中NaiveUI組件文字漸變的實(shí)現(xiàn)
這篇文章主要介紹了Vue.js中NaiveUI組件文字漸變的實(shí)現(xiàn),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07

