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>
posted @   7c89  阅读(40)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示