溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue3.2+ts如何實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗

發(fā)布時間:2022-12-15 09:45:07 來源:億速云 閱讀:112 作者:iii 欄目:開發(fā)技術(shù)

這篇“vue3.2+ts如何實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue3.2+ts如何實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗”文章吧。

直接上圖。

vue3.2+ts如何實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗

解決代碼如下:

import { createApp } from 'vue'
import MessageBoxVue from './MessageBox.vue' \\你寫的擬態(tài)框樣式
export const MessageBox = (text: any) => {
    return new Promise((resolve, reject) => {
        const capp = createApp(MessageBoxVue, text)
        const container = document.createElement('div')
        const instance = capp.mount(container)
        document.body.insertBefore(container, document.body.firstChild)//插入到body最前面,層級更高
        instance.callback = (val: any) => {
            if (val) {
                resolve(val)
            } else {
                reject()
            }
            capp.unmount()//注銷
            document.body.removeChild(container)//點擊后清除彈窗
        }
        instance.close = () => {
            capp.unmount()
            document.body.removeChild(container)
        }
    })
}
//重點在這兒
//這邊相當于把數(shù)據(jù)當方法使了。我也不知道什么原理。在上面那邊就是能接收到回調(diào)。
const cancel = () => {
    callback.value(false)
}
const confirm = () => {
    callback.value(true)
}
const closeBox = () => {
    close.value()
}
//拋出這兩個方法?反正這個方法不可或缺。
defineExpose({
    callback,
    close
})
\\這一段是上面的那個messagebox
<template>
    <div ref="MessageBox" class="message-box" :>
        <div class="message-box-icon">
            <i class="iconfont" : v-html="iconType"></i>
        </div>
        <div class="message-box-container">
            <div class="message-box-title">{{ title }}</div>
            <div v-if="isHtml" v-html="content"></div>
            <p v-else class="message-box-content">{{ content }}</p>
            <div class="message-box-btn">
                <span v-if="isCancel" class="cancel" @click="cancel">{{ cancelVal }}</span>
                <span v-if="isConfirm" class="confirm" @click="confirm">{{ confirmVal }}</span>
            </div>
        </div>
        <i v-if="closeIcon" class="iconfont cencel-box" @click="closeBox">&#xe618;</i>
    </div>
</template>
<script setup lang="ts">
import { computed, CSSProperties, ref } from 'vue'
const props = defineProps({
    type: {
        type: String,
        default: 'warning'
    },
    title: {
        type: String,
        default: ''
    },
    content: {
        type: String,
        default: ''
    },
    isHtml: {
        type: Boolean,
        default: false
    },
    width: {
        type: Number,
        default: 416
    },
    isCancel: {
        type: Boolean,
        default: true
    },
    cancelVal: {
        type: String,
        default: '取消'
    },
    confirmVal: {
        type: String,
        default: '確定'
    },
    isConfirm: {
        type: Boolean,
        default: true
    },
    closeIcon: {
        type: Boolean,
        default: false
    }
})
const callback = ref()
const close = ref()
const cw = document.documentElement.clientWidth
const ch = document.documentElement.clientHeight
// eslint-disable-next-line vue/return-in-computed-property
const iconType = computed(() => {
    switch (props.type) {
        case 'warning':
            return '&#xe603;'
        case 'info':
            return '&#xe601;'
        case 'error':
            return '&#xe602;'
        case 'success':
            return '&#xe604;'
    }
})
// eslint-disable-next-line vue/return-in-computed-property
const iconColor = computed(() => {
    switch (props.type) {
        case 'warning':
            return '#FF7402'
        case 'info':
            return '#0C64EB'
        case 'error':
            return '#FF4D4F'
        case 'success':
            return '#36B23B'
    }
})
const messageBoxWrapperStyleStyle = computed<CSSProperties>(() => {
    return {
        top: `${ch / 2 - 200 > 200 ? ch / 2 - 200 : 200}px`,
        left: `${cw / 2 - props.width / 2}px`,
        width: `${props.width}px`
    }
})
const cancel = () => {
    callback.value(false)
}
const confirm = () => {
    callback.value(true)
}
const closeBox = () => {
    close.value()
}
defineExpose({
    callback,
    close
})
</script>
<style lang="scss" scoped>
.cencel-box {
    position: absolute;
    top: 15px;
    right: 20px;
    cursor: pointer;
}
.message-box {
    position: absolute;
    border-radius: 5px;
    z-index: 2001;
    display: flex;
    background: #ffffff;
    padding: 20px 20px 20px 32px;
    box-shadow: 0px 0px 20px 0px rgba(6, 0, 1, 0.1);
    .message-box-icon {
        margin-right: 12px;
        line-height: 38px;
        .iconfont {
            font-size: 25px;
        }
    }
    .message-box-container {
        width: calc(100% - 25px);
        .message-box-title {
            font-size: 16px;
            color: #333333;
            line-height: 38px;
        }
        .message-box-content {
            margin: 10px 0px 20px;
            font-size: 14px;
            min-height: 48px;
            color: #999999;
            line-height: 18px;
            // letter-spacing: 0.5px;
        }
    }
    .message-box-btn {
        float: right;
        .cancel {
            height: 32px;
            line-height: 30px;
            display: inline-block;
            text-align: center;
            width: 90px;
            box-sizing: border-box;
            border: 1px solid #d9d9d9;
            border-radius: 3px;
            color: #333333;
            cursor: pointer;
        }
        .cancel:hover {
            color: #0c64eb;
            border-color: #0c64eb;
        }
        .confirm {
            height: 32px;
            width: 90px;
            display: inline-block;
            text-align: center;
            line-height: 32px;
            margin-left: 10px;
            background-color: #0c64eb;
            border-radius: 3px;
            color: #ffffff;
            cursor: pointer;
        }
        .confirm:hover {
            background-color: #2373eb;
        }
    }
}
</style>

引入全局后的調(diào)用

// 使用:
// 在main.ts全局引入,也可以按需引入這里展示全局引入
 import {MessageBox} from 'base/src/components/MessageBox/index'
// 注冊:
 app.config.globalProperties.$messageBox = MessageBox
// 引用:
 import { getCurrentInstance } from 'vue'
 const { proxy } = getCurrentInstance()
 const fn = ()=>{
     proxy
         .$messageBox({
		 //這邊就是傳你在MessageBox的props定義的父傳子的數(shù)據(jù)了
             title: '是否確定編輯/刪除數(shù)據(jù)?',
             content: '作業(yè)已經(jīng)發(fā)布,如果重新編輯/刪除,會清空所有學生 作業(yè)提交狀態(tài)請謹慎操作!'
         })
        .then(() => {
            console.log(1234)
         })
         .catch(() => {
             console.log(12345)
         })
}

以上就是關(guān)于“vue3.2+ts如何實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI