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

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

 更新時間:2022年12月14日 16:20:26   作者:Chaplink  
這篇文章主要介紹了vue3.2+ts實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗(類el-MessageBox),這個需求最主要的是要通過方法去調(diào)用,為了像el-messagebox使用那樣方便,需要的朋友可以參考下

公司UI設(shè)計的擬態(tài)框彈窗跟Element Plus UI的布局不太一致。導(dǎo)致不能夠直接修改樣式得到想到樣式。直接上圖。

這個需求最主要的是要通過方法去調(diào)用。為了像el-messagebox使用那樣方便。能直接在方法中調(diào)用。但這也是難點。去查看了element源碼,一個套一個的方法 實在看不懂。因為vue2和vue3的機制改動了。vue2的引用方式不適用了。查找了n篇文章,最終在一篇中找到了一個關(guān)鍵點,也可能是錯誤的方式,但是能用。

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)
        }
    })
}
//重點在這兒
//這邊相當(dāng)于把數(shù)據(jù)當(dāng)方法使了。我也不知道什么原理。在上面那邊就是能接收到回調(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" :style="[messageBoxWrapperStyleStyle]">
        <div class="message-box-icon">
            <i class="iconfont" :style="`color:${iconColor}`" 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ā)布,如果重新編輯/刪除,會清空所有學(xué)生 作業(yè)提交狀態(tài)請謹(jǐn)慎操作!'
         })
        .then(() => {
            console.log(1234)
         })
         .catch(() => {
             console.log(12345)
         })
}

到此這篇關(guān)于vue3.2+ts實現(xiàn)在方法中可調(diào)用的擬態(tài)框彈窗(類el-MessageBox)的文章就介紹到這了,更多相關(guān)vue3.2+ts實現(xiàn)擬態(tài)框彈窗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論