封裝vue彈窗組件,適用于手機端

封裝vue彈窗組件,效果圖如下:

66666.png

代碼目錄如下:

1603853047(1).jpg

components/Message/index.js

import Message from './Message.vue';
const MESSAGE = {
    install(Vue) {
        function dialog(options = {}) {
            return new Promise((resolve, reject) => {
                let MessageConstructor = Vue.extend(Message);
                //instance相當于Message.vue的this
                let instance = new MessageConstructor({
                    data: options,
                });
                instance.resolve = resolve; //綁定在Message.vue實例上
                instance.reject = reject;
                instance.$mount();
                document.body.appendChild(instance.$el);
                Vue.nextTick(() => {
                    instance.visible = true;
                });
            });
        }
        Vue.prototype.$message = dialog; // 掛載到vue原型上,暴露一個方法
    }
}
export default MESSAGE;

components/Message/Message.vue

<template>
    <transition name="fade" @after-leave="handleAfterLeave">
        <div class="message" v-show="visible">
            <div class="dialog">
                <div class="title" v-if="title">{{title}}</div>
                <div class="content">{{content || "請?zhí)顚懱崾緝热荩?}}</div>
                <div class="button-ground">
                    <button class="button" @click="cancel" :style="{color:cancelColor}" v-if="showCancel">
                        <p class="ellipsis">{{cancelText || "取消"}}</p>
                    </button>
                    <button class="button" @click="confirm" :style="{color:confirmColor}">
                        <p class="ellipsis">{{confirmText || "確定"}}</p>
                    </button>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
export default {
    name: 'message',
    data() {
        return {
            visible:false,//顯示與隱藏
            showCancel:true,//是否顯示取消按鈕
            title:null,//提示的標題
            content:null,//提示的內容
            cancelText:null,//取消按鈕的文字
            cancelColor:null,//取消按鈕的文字顏色
            confirmText:null,//確認按鈕的文字
            confirmColor:null,//確認按鈕的文字顏色
        }
    },
    methods: {
        confirm(){
            this.close('confirm');
        },
        cancel(){
            this.close('cancel');
        },
        close(action){
            this.visible = false;
            if(typeof this.callback === 'function'){
                //使用callback接收
                this.callback(action);
                this.resolve(action);//改promise的pending狀態(tài)
            }else{
                //使用then和catch接收
                if (action === 'confirm') {
                    this.resolve(action);
                } else if (action === 'cancel') {
                    this.reject(action);
                }
            }
        },
        handleAfterLeave(){
            document.body.removeChild(this.$el); //從body中移除dom,將v-show換成v-if也能移除dom
            this.$destroy(true);//銷毀組件
        }
    },
}
</script>
<style scoped lang="scss">
.fade-enter-active,.fade-leave-active{
    opacity: 1;
    transition: opacity .3s;
}
.fade-enter,.fade-leave-to{
    opacity: 0;
}
.ellipsis{
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
}
.message{
    user-select: none;
    -webkit-user-select: none;
    position: fixed;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    z-index: 999999;
    background: rgba($color: #000, $alpha: 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    .dialog{
        width: 100%;
        max-width: 320px;
        background: #fff;
        overflow: hidden;
        border-radius: 6px;
        padding-top:25px;
        margin-bottom: 60px;
        .title{
            font-size: 16px;
            font-weight: bold;
            color: #000;
            text-align: center;
            padding: 0px 20px;
            margin-bottom: 18px;
        }
        .content{
            font-size: 16px;
            color: #333;
            text-align: center;
            padding: 0px 20px;
            margin-bottom: 25px;
            line-height: 1.5;
        }
        .button-ground{
            position: relative;
            display: flex;
            &::before{
                content: '';
                display: block;
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                border-top: 1px solid #f5f5f5;
                transform: scaleY(0.5);
            }
            .button{
                position: relative;
                cursor: pointer;
                flex-grow: 1;
                display: flex;
                justify-content: center;
                align-items: center;
                outline: none;
                padding: 20px 10px;
                font-size: 16px;
                min-width: 50%;
                &:active{
                    background: rgba($color: #f5f5f5, $alpha: 1);
                    opacity: 0.8;
                }
                &:nth-of-type(n+2){
                    &::before{
                        content: '';
                        display: block;
                        position: absolute;
                        top: 0;
                        bottom: 0;
                        left: 0;
                        border-left: 1px solid #f5f5f5;
                        transform: scaleX(0.5);
                    }
                }
            }
        }
    }
}
</style>

在main.js中引入components/Message/index.js

import Vue from 'vue';
import App from './App.vue';
import Message from './components/Message/index';
Vue.use(Message);
new Vue({
    render: h => h(App),
}).$mount('#app');

使用方式:

//使用方式一:
this.$message({
    title:'溫馨提示',
    content:'你確定刪除這張圖片嗎?',
    confirmText:'刪除',
    confirmColor:'#ff0',
    cancelColor:'#f00',
    cancelText:'取消',
    showCancel:true,
    callback:(action)=>{
        if(action === 'confirm'){
            //確定
        }else{
            //取消
        }
    }
});

//使用方式二:
this.$message({
    content:'是否開通VIP用戶?',
    confirmText:'開通',
    confirmColor:'#ff0',
    showCancel:false,
}).then((action)=>{
    //確定
}).catch((action)=>{
    //取消
});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容