html使用element ui_仿element-ui(message) 使用vue.extend() 简单封装 vue插件

1.在根目录 -新建文件夹: message。 2.新建-vue文件: message.vue。
<template>
  <div class="message-content" v-show="show" :class="type">
       <span class="text">{{text}}</span>
  </div>
</template>

<script>
export default {
  name: '',
  props:{
    type:{
      type:String,
      default:''
    },
    text:{
      type:String | Object,
      default:''
    },
    show:{
      type:Boolean,
      default:false
    }

  },
  data() { 
    return {

    }
  }
 }
</script>

<style  scoped>
.message-content{
  position: fixed;
  right: 0px;
  top:40px;
  min-width: 400px;
  padding: 10px 20px;
  background-color: #555;
  color:#333;
}
.message-content.info{
  background-color: indianred;
}
.message-content.success{
  background-color: saddlebrown;
}
</style>
3.新建js index.js。
import msgs from './message.vue'
const MESSAGE ={
  show:true,
  duration:1000,
  install(Vue) {
      if (typeof window !== 'undefined' && window.Vue) {
        Vue = window.Vue
      }
      Vue.component('Message', msgs);
      function msg(type, text, callBack) {
            let msg
            let duration = MESSAGE.duration
            if (typeof text === 'string') {
              msg = text
            } else if (text instanceof Object) {
              msg = text.text || ''
              if (text.duration) {
                duration = text.duration
              }
          }
          let VueMessage =  Vue.extend({
              render(h) {
                  let props = {
                    type,
                    text:msg,
                    show: this.show
                  }
                  return h(msgs, {
                    props
                  })
              },
              data() {
                return {
                  show: false
                }
              }

            })

            let newMessage = new VueMessage()
            let vm = newMessage.$mount()
            console.log(vm,'istall  vmmm')
            let el = vm.$el
            document.body.appendChild(el) // 把生成的提示的dom插入body中
            vm.show = true
            let t1 = setTimeout(() => {
              clearTimeout(t1)
              vm.show = false //隐藏提示组件,此时会有300ms的动画效果,等动画效果过了再从body中移除dom
              let t2 = setTimeout(() => {
                clearTimeout(t2)
                document.body.removeChild(el) //从body中移除dom
                newMessage.$destroy()
                vm = null // 设置为null,好让js垃圾回收算法回收,释放内存

                callBack && (typeof callBack === 'function') && callBack()
              }, MESSAGE.duration)
            }, MESSAGE.duration)



      }
      Vue.prototype.$message = {
        info(text, callBack) {
          if (!text) return
          msg('info', text, callBack)
        },
        success(text, callBack) {
          if (!text) return
          msg('success', text, callBack)
        },
        error(text, callBack) {
          if (!text) return
          msg('error', text, callBack)
        },
        warning(text, callBack) {
          if (!text) return
          msg('warning', text, callBack)
        }
      }

  } 
}
export default MESSAGE
4.main.js 引入
import message from './message /index.js'
Vue.use(message );

5.组件内使用

该插件,支持四种类型的 message提示框,message默认延迟:1s, 样式自行修改。

 this.$message.info('韦德牌男人', function callBack(){
           alert ('我消失了')
 }) 
 
 this.$message.success('韦德牌男人', function callBack(){
           alert ('我消失了')
 })

 this.$message.error('韦德牌男人', function callBack(){
           alert ('我消失了')
 })  

 this.$message.warning('韦德牌男人', function callBack(){
           alert ('我消失了')
 })

例如:
内容:对象(类型)

 mounted () {
    this.$message.success({text:'韦德拍男人',duration:3000} , function callBack(){
           alert ('我消失了')
     })
 } 

内容:字符串(类型)

 mounted () {
     this.$message.success('韦德牌男人', function callBack(){
           alert ('我消失了')
     })
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值