如何编写一个抢购bot_如何编写一个 vue toast 插件

1 编写一个 vue 模板

2 通过 Vue.extend() 扩展模板

3 new 出实例

4 挂载 dom

5 向 body 添加 dom

6 向 Vue 原型添加 toast

编写 插件目录如下

20b0d40ebe507c1ace9dfaf78aeb3df5.png

fyChatToast.vue 代码如下

<template>
  <transition name="alert-fade">
    <div id="toast"
         v-show="visible"
         class="dialog-tips dialog-center">
      {{message}}
    </div>
  </transition>
</template>
<script>
export default {
  data () {
    return {
      visible: false,
      message: ''
    }
  }
}
</script>
<style lang="scss" scoped>
.alert-fade-enter-active,
.alert-fade-leave-active {
  transition: opacity 0.3s;
}
.alert-fade-enter,
.alert-fade-leave-to {
  opacity: 0;
}
.dialog-tips {
  position: fixed;
  z-index: 100;
  min-width: 100px;
  padding: 15px;
  border-radius: 15px;
  white-space: nowrap;
  background-color: rgba(0,0,0,1);
  box-shadow: 0px 8px 30px 0 rgba(0, 0, 0, 0.363);
  text-align: center;
  color: #fff;
  font-size: 15px
}
.dialog-center {
  top: 20%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>

index.js

import fyChatToast from './src/fyChatToast.vue'
const toast = {}
toast.install = Vue => {
    // 扩展 vue 插件
    const ToastCon = Vue.extend(fyChatToast)
    const ins = new ToastCon()
    // 挂载 dom
    ins.$mount(document.createElement('div'))
    // 添加到 body 后面
    document.body.appendChild(ins.$el)
    // 给 vue 原型添加 toast 方法
    Vue.prototype.$toast = (msg, duration = 3000) => {
        // 我们调用的时候 赋值 message
        // 将 visible 设置为 true
        // 默认 3s 之后 设置 为 false 关闭 toast
        ins.message = msg
        ins.visible = true
        setTimeout(() => {
            ins.visible = false
        }, duration)
    }
}
export default toast

在 main.js 引入

e3d9678785da7205602c45534e497ded.png

在代码中使用

e32b6a7923cbb6e2956ef5886d921d70.png

最终效果

2918c4260f43464bbaa117a012368aef.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值