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

實現(xiàn)思路
1、dialog組件為模態(tài)框,因此應該是固定定位到頁面上面的,并且需要留一定的插槽來讓使用者自定義顯示內(nèi)容
2、難點在于如何一句話打開dialog,也就是下面的index.js文件的內(nèi)容:導入我們已經(jīng)寫好的組件(可以先寫一個及其簡單的),模塊暴露出一個函數(shù)(DiaLog)用于生成dialog,這里主要利用到vue中的createApp函數(shù),createApp創(chuàng)建應用,將應用掛載到我們的新建div標簽上,隨著用戶觸發(fā)點擊事件,將div標簽銷毀即可
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:'我不想起標題'})">起飛</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">確認</button>
</div>
</div>
<div class="background" v-if="props._background"></div>
</template>
<script setup>
const props = defineProps({
_title: {
type: String,
default: '無標題'
},
_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>
到此這篇關于Vue手寫dialog組件模態(tài)框過程詳解的文章就介紹到這了,更多相關Vue dialog組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue設置全局變量5種方法(讓你的數(shù)據(jù)無處不在)
這篇文章主要給大家介紹了關于vue設置全局變量的5種方法,通過設置的方法可以讓你的數(shù)據(jù)無處不在,在項目中經(jīng)常會復用一些變量和函數(shù),比如用戶的登錄token,用戶信息等,這時將它們設為全局的就顯得很重要了,需要的朋友可以參考下2023-11-11
解決vue-quill-editor上傳內(nèi)容由于圖片是base64的導致字符太長的問題
vue-quill-editor默認插入圖片是直接將圖片轉為base64再放入內(nèi)容中,如果圖片較多,篇幅太長,就會比較煩惱,接下來通過本文給大家介紹vue-quill-editor上傳內(nèi)容由于圖片是base64的導致字符太長的問題及解決方法,需要的朋友可以參考下2018-08-08
解決vue prop傳值default屬性如何使用,為何不生效的問題
這篇文章主要介紹了解決vue prop傳值default屬性如何使用,為何不生效的問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

