自定义 toast (vue.extend)

自定义Toast

toast.vue

<template>
  <div class="toast" v-if="showWrap">
    <div class="toast_icon">
      <img v-if="iconState === 'S'" src="../assets/images/success.png" alt="">
      <img v-if="iconState === 'F'" src="../assets/images/fail.png" alt="">
    </div>
    <div class="toast_text">{{text}}</div>
  </div>
</template>

<script>
export default {
  name: 'toast',
  props: {
  },
  data () {
    return {
    }
  },
  methods: {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  min-width: 360px;
  padding: 30px 40px;
  transform: translate(-50%, -50%);
  border-radius: 20px;
  background: rgba($color: #000000, $alpha: 0.8);
  z-index: 3001;
  overflow: hidden;
  .toast_icon {
    width: 40px;
    height: 40px;
    margin: 0px auto 10px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .toast_text {
    text-align: center;
    color: #fff;
    font-size: 18px;
    line-height: (30/18);
    white-space: nowrap;
  }
}

</style>

toast.js

import vue from 'vue';
import toastComponent from '../components/toast.vue'
const ToastConstructor = vue.extend(toastComponent)
var Test = true
// 定义弹出组件的函数 接收2个参数, 要显示的文本 和 显示时间
let toast = {
  show: (text, iconState, duration = 1500) => {
    if (Test === false) {
      return
    }
    const toastDom = new ToastConstructor({
      el: document.createElement('div'),
      data () {
        return {
          text: text,
          showWrap: true, // 是否显示组件
          showContent: true, // 作用:在隐藏组件之前,显示隐藏动画
          iconState: iconState
        }
      }
    })
    document.body.appendChild(toastDom.$el)
    Test = false
    // 过了 duration 时间后隐藏整个组件
    setTimeout(() => {
      toastDom.showWrap = false
      Test = true
    }, duration)
  },

  success: (text) => {
    text = text? text : '操作成功'
    toast.show(text, 'S')
  },

  fail: (text) => {
    text = text? text : '操作失败'
    toast.show(text, 'F')
  }
}

vue.prototype.$toast = vue.$toast = toast

export default toast

使用

this.$toast.success()
this.$toast.fail('操作失败')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值