最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

<script setup> 就是 setup 語法糖

defineProps 和 props 用法差不多

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

手寫彈窗組件

很簡單的彈窗組件,支持設(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)建一個實例,掛載到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)組件調(diào)用和函數(shù)式調(diào)用合二為一了。

如何使用

<script setup>
import Dialog from '@/components/Dialog.js'
// 無標(biāo)題
Dialog('500 服務(wù)器錯誤,請稍候再試!');
// 有標(biāo)題
Dialog('500 服務(wù)器錯誤,請稍候再試!', { 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)用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue中的localStorage使用方法詳解

    vue中的localStorage使用方法詳解

    在Vue項目中可以直接使用localStorage,它支持Vue2和Vue3,在Vue2中,可以通過`localStorage.setItem()`、`localStorage.getItem()`和`localStorage.removeItem()`來保存、讀取和刪除數(shù)據(jù),本文給大家介紹vue中的localStorage使用詳解,感興趣的朋友一起看看吧
    2025-03-03
  • web前端vue filter 過濾器

    web前端vue filter 過濾器

    vue的過濾器通常用在一些常見的文本格式化,過濾器可以用在兩個地方:雙花括號插值和 v-bind 表達(dá)式。本文給大家介紹了web前端vue filter 過濾器的相關(guān)知識,感興趣的朋友一起看看吧
    2018-01-01
  • 基于Vue 擼一個指令實現(xiàn)拖拽功能

    基于Vue 擼一個指令實現(xiàn)拖拽功能

    這篇文章主要介紹了Vue 指令實現(xiàn)拖拽功能,實現(xiàn)原理很簡單,文中通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • 詳解vue項目打包后通過百度的BAE發(fā)布到網(wǎng)上的流程

    詳解vue項目打包后通過百度的BAE發(fā)布到網(wǎng)上的流程

    這篇文章主要介紹了將vue的項目打包后通過百度的BAE發(fā)布到網(wǎng)上的流程,主要運用的技術(shù)是vue+express+git+百度的應(yīng)用引擎BAE。需要的朋友可以參考下
    2018-03-03
  • 詳解Element-UI中上傳的文件前端處理

    詳解Element-UI中上傳的文件前端處理

    這篇文章主要介紹了詳解Element-UI中上傳的文件前端處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • vue中復(fù)用vuex.store對象的定義

    vue中復(fù)用vuex.store對象的定義

    這篇文章主要介紹了vue中復(fù)用vuex.store對象的定義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue使用el-tree 懶加載進(jìn)行增刪改查功能的實現(xiàn)

    Vue使用el-tree 懶加載進(jìn)行增刪改查功能的實現(xiàn)

    這篇文章主要介紹了Vue使用el-tree 懶加載進(jìn)行增刪改查,以懶加載的形式展示,目錄根據(jù)需求需要有新增 編輯 刪除 操作以及操作后的刷新樹結(jié)構(gòu),具體實現(xiàn)代碼跟隨小編一起看看吧
    2021-08-08
  • vue2.0 解決抽取公用js的問題

    vue2.0 解決抽取公用js的問題

    這篇文章主要介紹了vue2.0 解決抽取公用js的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue.js中 v-model 指令的修飾符詳解

    Vue.js中 v-model 指令的修飾符詳解

    v-model 指令默認(rèn)會在 input 事件中加載輸入框中的數(shù)據(jù)(中文輸入法中輸入拼音的過程除外)。這篇文章通過實例代碼給大家介紹Vue.js中 v-model 指令的修飾,感興趣的朋友跟隨小編一起看看吧
    2018-12-12
  • 自帶氣泡提示的vue校驗插件(vue-verify-pop)

    自帶氣泡提示的vue校驗插件(vue-verify-pop)

    這篇文章主要為大家詳細(xì)介紹了自帶氣泡提示的vue校驗插件,vue-verify-pop的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評論

永泰县| 合山市| 邵东县| 大悟县| 逊克县| 连江县| 宜章县| 自贡市| 高雄市| 罗源县| 淳安县| 翁源县| 武功县| 抚顺县| 池州市| 印江| 临清市| 安徽省| 浮山县| 牟定县| 西乡县| 广汉市| 阿巴嘎旗| 江源县| 广安市| 济南市| 长乐市| 张家口市| 班戈县| 垫江县| 洛南县| 肥西县| 太保市| 徐汇区| 东乡族自治县| 阳朔县| 汉阴县| 枝江市| 上蔡县| 平远县| 龙陵县|