Vue.extend 编写 Toast

2022/2/9 23:15:40

本文主要是介绍Vue.extend 编写 Toast,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

home.vue

<template>
  <div>
    <el-button @click="toToast"> 显示 </el-button>
  </div>
</template>

<script>
import { showToast } from "./toast.js";
export default {
  methods: {
    toToast() {
      showToast("测试", 1500);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>

toast.js

import Vue  from "vue";
import Toast from './toast.vue'
const ToastCtor=Vue.extend(Toast)
export function showToast(content,delay){
    const toastComp=new ToastCtor().$mount()
    document.body.appendChild(toastComp.$el)
    toastComp.toShow(content)
    setTimeout(() => {
        toastComp.toClose()
        toastComp.$el.parentNode.removeChild(toastComp.$el) 
    }, delay);
}

toast.vue

<template>
  <!-- <div class="toast" v-if="show">{{message}}</div> -->
  <div class="toast" v-show="show">{{message}}</div>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      message:'',
    };
  },
  methods: {
    toShow(content) {
        this.message=content
      this.show = true;
    },
    toClose() {
      this.show = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.toast {
  position: fixed;
  top: 10px;
  left: 50%;
  padding: 10px 20px;
  background: rgba(22, 21, 21, 0.658);
  color: #fff;
  font-size: 14px;
  border-radius: 6px;
  min-width: 80px;
  text-align: center;
}
</style>


这篇关于Vue.extend 编写 Toast的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程