vue封装全局提示组件

由于现有组件的提示框样式无法满足UI的要求,所以自己封装一个全局提示框,
效果如下

在这里插入图片描述

1.首先写一个vue组件

<template>
      <!-- 全局提示框 -->
      <transition >
          <div v-show="visible" class="myNotifyComponent">
             <div class="msg">{{message}}</div>
          </div>
      </transition>
     
  
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      message: "",
    };
  }
};
</script>
<style lang="less" scoped>
.myNotifyComponent{
    position: fixed;
    top: 68px;
    left: 50%;
    transform: translate(-50%,0);
    z-index: 9999;
    min-width: 220px;
    max-width: 700px;
    height: 82px;
    line-height: 82px;
    border-radius: 99999px;
    padding: 0 20px;
    white-space: nowrap;
    background-color: @notify;
    text-align: center;
    color:#fff ;
    font-size: 26px;
}
   //过渡动画
    .v-enter, .v-leave-to {
       opacity: 0;
       transform: translate(-50%,-68px)
    }
    .v-enter-active, .v-leave-active {
       transition: all .5s
    }

</style>

2.实现动态加载组件

import myNotifyComponent from './index.vue'

const myNotify = {};

// 注册myNotify
myNotify.install = function (Vue) {
    // 生成一个Vue的子类
    // 同时这个子类也就是组件
    const myNotifyConstructor = Vue.extend(myNotifyComponent)
    // 生成一个该子类的实例
    const instance = new myNotifyConstructor();

    // 将这个实例挂载在我创建的div上
    // 并将此div加入全局挂载点内部
    instance.$mount(document.createElement('div'))
    document.body.appendChild(instance.$el)

    // 通过Vue的原型注册一个方法
    // 让所有实例共享这个弹出组件的方法,接收两个参数:要显示的文本 和 显示时间
    Vue.prototype.$myNotify = (msg, duration = 1500) => {
        instance.message = msg;
        instance.visible = true;

        setTimeout(() => {
            instance.visible = false;
        }, duration);
    }
}

export default myNotify

3.在入口文件main.js中引入使用

import myNotify from '@/components/myNotify/myNotify'
Vue.use(myNotify)

4.使用

this.$myNotify("登录成功")
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值