vue自定义全局组件,vue自定义全局消息弹窗,vue自定义全局消息提示,一步步来,别着急

先看一下我的目录结构:
vue自定义全局组件,vue自定义全局消息弹窗,vue自定义全局消息提示,一步步来,别着急_第1张图片

  1. 首先和普通组件一样定义一个自定义组件在components下面:CusConfirm
<template>
    <transition name="fade">
        <div class="cus-confirm-mask" v-if="flag" @click="cancel">
            <div class="cus-confirm" @click.stop>
                <div class="confirm-title">确认提示:div>
                <div class="confirm-body">{{title}}div>
                <div class="confirm-btns">
                    <button class="cancel-btn" @click="no">取消button>
                    <button class="ok-btn" @click="sure">确认button>
                div>
            div>
        div>
    transition>
template>

<script>
export default {
    data() {
        return {
            flag: false,
            title: ""
        }
    },
    methods: {
        no() {
            this.flag = false;
            this.cancel();
        },
        sure() { 
            this.flag = false;
            this.ok();
        }
    }
};
script>

<style lang="less" scoped>
// 渐变过渡
.fade-enter,
.fade-leave-active {
    opacity: 0;
}
.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.35s;
}
.cus-confirm-mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    .cus-confirm {
        width: 480px;
        height: 260px;
        box-shadow: 0px 10px 30px 0px rgba(0, 0, 0, 0.2);
        position: absolute;
        top: calc(50% - 130px);
        left: calc(50% - 240px);
        border-radius: 12px;
        padding: 15px;
        z-index: 999;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        background: #ffffff;
        .confirm-title {
            font-size: 28px;
        }
        .confirm-body {
            text-align: center;
            font-size: 18px;
        }
        .confirm-btns {
            text-align: right;
            button {
                border: 0;
                outline: none;
                padding: 6px 18px;
                border-radius: 6px;
                margin: 0 12px;
                font-size: 16px;
                cursor: pointer;
            }
            .ok-btn {
                background: #f5f5f5;
                color: #fc9541;
            }
            .cancel-btn {
                background: #fc9541;
                color: #f5f5f5;
            }
            .ok-btn:active {
                background: #e5e5e5;
            }
            .cancel-btn:active {
                background: #fb7a12;
            }
        }
    }
}
style>
  1. 看同级的index.js
import Vue from 'vue'
import CusConfirm from './CusConfirm.vue'

const Confirm = Vue.extend(CusConfirm)

CusConfirm.install = function (options) {
    /**
     * options的其他情况自行判断添加默认值等等
     */
    if (options === undefined || options === null) {
        options = {
            title: '是否确认?'
        }
    } else if (typeof options === 'string' || typeof options === 'number') {
        options = {
            title: options
        }
    }

    let instance = new Confirm({
        data: options
    }).$mount()

    document.body.appendChild(instance.$el)

    Vue.nextTick(() => {
        instance.flag = true //记得这里是控制弹窗显隐的状态,一定要同自定义组件里边 v-if 后面的值相同
    })
}

export default CusConfirm
  1. 在main.js里面引入,类似于使用第三方插件那样
//自定义全局组件
import CusConfirm from './components/CusConfirm'
Vue.prototype.$confirm = CusConfirm.install;
  1. 然后就可以在任何地方使用了,比如我的退出按钮:
<template>
    <div class="common-header">
		<span class="logout-text" @click="showConfirm">退出span>
    div>
template>
<script>
export default {
    methods:{
        showConfirm(){
            console.log('退出');
            this.confirmFlag = true;
            this.$confirm({
                title:"是否确认退出?",
                cancel:()=>{
                    console.log('点取消调用');
                },
                ok:()=>{
                    console.log('点确定调用');
                    this.logout();
                }
            });
        },
        logout() {
            let params = {};
            params.uid = this.storage.getItem("uid");
            this.$axios
                .post(this.$api.loginlogout, params)
                .then(res => {
                    if (res.data.code_status == 0) {
                        this.storage.clearAll();
                        this.$router.push("/login");
                    } else {
                        this.$Message.error(res.data.msg);
                    }
                })
                .catch(err => {
                    this.$Message.error("网络错误,请重试");
                });
        },
    }
};
script>

你可能感兴趣的:(vue,vue)