欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

涉及的vue3知識(shí)點(diǎn)/API,createApp defineProps defineEmits <script setup> v-model

<script setup> 就是 setup 語(yǔ)法糖

defineProps 和 props 用法差不多

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

手寫彈窗組件

很簡(jiǎn)單的彈窗組件,支持設(shè)置標(biāo)題

<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)">確認(rèn)</button>
            </div>
        </div>
    </div>
</template>

組件調(diào)用

在需要使用的地方引入組件,v-model:show 相當(dāng)于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="這是標(biāo)題"></Dialog>
</template>

函數(shù)式調(diào)用

components目錄下新建Dialog.js文件

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

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

如何使用

<script setup>
import Dialog from '@/components/Dialog.js'
// 無(wú)標(biāo)題
Dialog('500 服務(wù)器錯(cuò)誤,請(qǐng)稍候再試!');
// 有標(biāo)題
Dialog('500 服務(wù)器錯(cuò)誤,請(qǐng)稍候再試!', { 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)">確認(rèn)</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自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于vue3.2彈窗組件函數(shù)式調(diào)用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue中動(dòng)態(tài)出來(lái)返回的時(shí)間秒數(shù)(在多少秒顯示分、小時(shí)等等)

    vue中動(dòng)態(tài)出來(lái)返回的時(shí)間秒數(shù)(在多少秒顯示分、小時(shí)等等)

    這篇文章主要給大家介紹了關(guān)于vue中動(dòng)態(tài)出來(lái)返回的時(shí)間秒數(shù)(在多少秒顯示分、小時(shí)等等)的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • vue print.js打印支持Echarts圖表操作

    vue print.js打印支持Echarts圖表操作

    這篇文章主要介紹了vue print.js打印支持Echarts圖表操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue2.0如何實(shí)現(xiàn)echarts餅圖(pie)效果展示

    vue2.0如何實(shí)現(xiàn)echarts餅圖(pie)效果展示

    這篇文章主要介紹了vue2.0如何實(shí)現(xiàn)echarts餅圖(pie)效果展示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue中的.vue文件的使用方式

    Vue中的.vue文件的使用方式

    這篇文章主要介紹了Vue中的.vue文件的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue3?Error:Unknown?variable?dynamic?import:?../views/的解決方案

    vue3?Error:Unknown?variable?dynamic?import:?../views/的解

    這篇文章主要給大家介紹了關(guān)于vue3?Error:Unknown?variable?dynamic?import:?../views/的解決方案,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Vue3前端生成隨機(jī)id(生成?UUID)實(shí)際運(yùn)用

    Vue3前端生成隨機(jī)id(生成?UUID)實(shí)際運(yùn)用

    前端在做增刪改查時(shí)通常會(huì)使??個(gè)唯?數(shù)值做為數(shù)據(jù)的key值,下面這篇文章主要給大家介紹了關(guān)于Vue3前端生成隨機(jī)id(生成?UUID)的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-04-04
  • Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果的實(shí)例代碼

    Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果的實(shí)例代碼

    這篇文章主要介紹了Vue實(shí)現(xiàn)Header漸隱漸現(xiàn)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)

    詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)

    這篇文章主要介紹了詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 詳解Vue CLI3配置解析之css.extract

    詳解Vue CLI3配置解析之css.extract

    這篇文章主要介紹了詳解Vue CLI3配置解析之css.extract,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • vue使用Highcharts實(shí)現(xiàn)3D餅圖

    vue使用Highcharts實(shí)現(xiàn)3D餅圖

    這篇文章主要為大家詳細(xì)介紹了vue使用Highcharts實(shí)現(xiàn)3D餅圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論