Notice组件实现

组件实例创建函数:create函数

import Vue from 'vue'
export default function create (Component, props) {
    // 先创建实例
    const vm = new Vue({
        render (h) {
       //h就是createElement,它返回VNode
          return h(Component, {props})
        }
    }).$mount()
    // 手动挂载
    document.body.appendChild(vm.$el)
    //销毁方法
    const comp = vm.$children[0]
    comp.remove = function() {
      document.body.removeChild(vm.$el)
      vm.$destroy()
    }
    return comp
}

Notice组件

// 插槽预留
// 标题。内容等属性
// 自动关闭
<template>
  <div class="box" v-if="isShow">
    <h3>{{title}}</h3>
    <p class="box-content">{{message}}</p>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: ""
    },
    message: {
      type: String,
      default: ""
    },
    duration: {
      type: Number,
      default: 1000
    }
  },
  data() {
    return {
      isShow: false
    }
  },
  methods: {
    show(){
      this.isShow = true
      setTimeout(this.hide,this.duration);
    },
    hide() {
      this.isShow = false
      this.remove()
    }
  }
}
</script>

使用

<script>
import Notice from '@/components/notice/Notice'
export default {
  methods: {
    submitForm(form) {
      this.$refs[form].validate(valid => {
        const notice = this.$create(Notice,{
          title:'jdfksjf',
          message: valid ? "请求登录!" : "校验失败",
          duration:1000
        })
        notice.show()
      })
    }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值