vue 创建提示框公共组件

//vue自制提示框(借鉴别人的)
//创建组件
<template>
  <div
    class="app-toast"
    v-if="isShow"
    :class="{
      info: type === 'info',
      success: type === 'success',
      wraning: type === 'wraning',
      danger: type === 'danger'
    }"
  >
    {{ text }}
  </div>
</template>
<style scoped>
.app-toast {
  position: fixed;
  left: 50%;
  top: 5%;
  background: #ccc;
  padding: 10px;
  border-radius: 5px;
  transform: translate(-50%, -50%);
  color: #fff;
}
.info {
  background: #00aaee;
}
.success {
  background: #00ee6b;
}
.wraning {
  background: #eea300;
}
.danger {
  background: #ee000c;
}
</style>
//创建 js
import vue from "vue";
import toastComponent from "@/components/VNotify.vue";

// 组件构造器,构造出一个 vue组件实例
const ToastConstructor = vue.extend(toastComponent);

function showToast({ text, type, duration = 3000 }) {
  const toastDom = new ToastConstructor({
    el: document.createElement("div"),
    data() {
      return {
        isShow: true, // 是否显示
        text: text, // 文本内容
        type: type // 类型
      };
    }
  });
  // 添加节点
  document.body.appendChild(toastDom.$el);
  // 过渡时间
  setTimeout(() => {
    toastDom.isShow = false;
  }, duration);
}
// 全局注册
function registryToast() {
  vue.prototype.$toast = showToast;
}

export default registryToast;
//main 中全局 引用
import toastRegistry from "./uitls/ntify";
Vue.use(toastRegistry);