Vue自定义组件(模仿iOS UIAlertActionCont

2020-12-10  本文已影响0人  Enjoyhy

1.在component文件夹中创建组件confirm.vue

<template>
    <div class="mask">
        <div class="confirm-wrap">
            <div class="title">{{msg}}</div>
            <div class="handel">
                <div v-for="(item,index) in btns" :key="index" class="btn" @click="handelClick(index)">{{item.text}}</div>
            </div>
        </div>
        <!-- <div class="confirm-wrap">
            <div class="title">确认删除?</div>
            <div class="handel">
                <div class="btn">取消</div>
                <div class="btn">确认</div>
            </div>
        </div> -->
    </div>
</template> 

<script>
export default {
    data() {
        return {
            msg: "",
            btns:[],
        }
    },
    methods: {
        handelClick(index) {
            this.btns[index].onPress()
        }
    },
}
</script>

<style lang="less" scoped>
.mask{
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    background-color: rgba(0,0,0,0.6);
    left: 0;
    top: 0;
    .confirm-wrap{
        width: 250px;
        height: 120px;
        background-color: #fff;
        position: absolute;
        border-radius: 10px;
        z-index: 1;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        .title{
            text-align: center;
            font-size: 14px;
            margin-top: 20px;
        }
        .handel{
            display: flex;
            justify-content: space-between;
            margin-top:20px;
            border-top: 1px solid #eee;
            .btn{
                width: 100%;
                text-align: center;
                padding: 18px;
            }
            .btn:first-child{
                color: #868686;
                border-right: 1px solid #eee;
            }
        }
    }
}
</style>

2.在component文件夹创建index.js

import Confirm from './confirm';
export default {
    install(Vue){
        // Vue.component("my-confirm",Confirm)
        let ConfirmObj = Vue.extend(Confirm)
        Vue.prototype.$confirm = function(msg, btns){
            let ConfirmInit = new ConfirmObj().$mount(document.createElement("div"))
            console.log(ConfirmInit)
            document.body.appendChild(ConfirmInit.$el)
            ConfirmInit.msg = msg
            ConfirmInit.btns = btns
            console.log(msg, btns)
        }
    }
}

3.在入口的main.js文件中如下配置:

import Confirm from './component';
Vue.use(Confirm)

4.在需要使用的组件中正常调用

<template>
    <div id="bg">
        <button @click="del">删除</button>
    </div>
</template>

<script>
import Vue from 'vue';
export default {
    
    methods: {
        del(){
            this.$confirm("确认要删除吗?",[
                {text:"取消",
                onPress :()=> {
                    console.log("取消")
                }},
                {text:"确认",
                onPress :()=> {
                    console.log("确认")
                }},
            ])
        }
    },

    
}
</script>

<style lang="less" scoped>
#bg{
    background-color: #fff;
}

</style>
自定义组件
上一篇下一篇

猜你喜欢

热点阅读