亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3.2自定義彈窗組件結合函數(shù)式調用示例詳解

 更新時間:2022年09月21日 09:17:00   作者:簡單卟容易  
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結合函數(shù)式調用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

涉及的vue3知識點/API,createApp defineProps defineEmits <script setup> v-model

<script setup> 就是 setup 語法糖

defineProps 和 props 用法差不多

defineEmits 聲明可向其父組件觸發(fā)的事件

手寫彈窗組件

很簡單的彈窗組件,支持設置標題

<script setup>
defineProps({
    show: {
        type: Boolean,
        default: false
    },
    title: {
        type: String,
        default: ''
    },
    message: {
        type: String,
        default: ''
    }
})
</script>
<template>
    <div v-if="show" class="dialog-mask flex-center">
        <div class="dialog-box">
            <div class="dialog-header">{{title}}</div>
            <slot><p class="dialog-content">{{message}}</p></slot>
            <div class="dialog-footer">
                <button class="button dialog-confirm" @click="$emit('update:show', false)">確認</button>
            </div>
        </div>
    </div>
</template>

組件調用

在需要使用的地方引入組件,v-model:show 相當于vue2寫法的 :show.sync 前方指路

<script setup>
import { ref } from 'vue'
import Dialog from '@/components/Dialog.vue'
let show = ref(true)
</script>
<template>
  <Dialog v-model:show="show" message="這是提示內(nèi)容" title="這是標題"></Dialog>
</template>

函數(shù)式調用

components目錄下新建Dialog.js文件

將上面寫好的組件引入,創(chuàng)建一個實例,掛載到body節(jié)點

import { createApp } from 'vue'
import Dialog from '@/components/Dialog.vue'
const createDialog = (message, option = {}) => {
    const mountNode = document.createElement('div')
    const Instance = createApp(Dialog, {
        show: true,
        message,
        ...option,
        close: () => { 
            Instance.unmount(mountNode); 
            document.body.removeChild(mountNode);
        }
    })
    document.body.appendChild(mountNode)
    Instance.mount(mountNode)
}
export default createDialog

createApp 第二個參數(shù),是傳遞prop給組件,close方法用于點擊確定移除彈窗,所以我們需要改造一下Dialog.vue,改造后的代碼在下面含樣式完整源碼里,改造后就能實現(xiàn)組件調用和函數(shù)式調用合二為一了。

如何使用

<script setup>
import Dialog from '@/components/Dialog.js'
// 無標題
Dialog('500 服務器錯誤,請稍候再試!');
// 有標題
Dialog('500 服務器錯誤,請稍候再試!', { title: '提示' });
</script>

含樣式完整源碼

Dialog.vue

<script setup>
defineProps({
    show: {
        type: Boolean,
        default: false
    },
    title: {
        type: String,
        default: ''
    },
    message: {
        type: String,
        default: ''
    },
    close: {
        type: Function,
        default: fun => fun()
    }
})
const emit = defineEmits(['update:show'])
const handleClose = () => {
    emit('update:show', false)
}
</script>
<template>
    <div v-if="show" class="dialog-mask flex-center">
        <div class="dialog-box">
            <div class="dialog-header">{{title}}</div>
            <slot><p class="dialog-content">{{message}}</p></slot>
            <div class="dialog-footer">
                <button class="button dialog-confirm" @click="close(handleClose)">確認</button>
            </div>
        </div>
    </div>
</template>
<style scoped>
.flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
}
.dialog-mask {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
}
.dialog-box {
    background: #fff;
    width: 300px;
    border-radius: 10px;
    overflow: hidden;
}
.dialog-header {
    padding-top: 20px;
    font-weight: bold;
    text-align: center;
}
.dialog-content {
    padding: 5px 20px 20px 20px;
    font-size: 12px;
    text-align: center;
    white-space: pre-wrap;
    word-wrap: break-word;
}
.dialog-footer {
    display: flex;
    overflow: hidden;
    user-select: none;
    border-top: 1px solid #EBEDF0;
}
.button {
    display: inline-block;
    box-sizing: border-box;
    text-align: center;
    width: 100%;
    line-height: 40px;
    background-color: #fff;
}
.button:active {
    background-color: #f2f3f5;
}
.dialog-confirm {
    color: #409EFF;
}
</style>

效果圖

以上就是vue3.2自定義彈窗組件結合函數(shù)式調用示例詳解的詳細內(nèi)容,更多關于vue3.2彈窗組件函數(shù)式調用的資料請關注腳本之家其它相關文章!

相關文章

最新評論