热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

vue3.x全局toast、message、loading组件

vue3.x全局toast、message、loading组件Toast组件loadingToast组件在srccomponents下创建toast文件夹,并依此创


vue3.x全局toast、message、loading组件

    • Toast组件
    • loading


Toast组件


  • 在 src/components下创建toast文件夹,并依此创建index.vue和index.js

1、index.vue
一般toast会有如下功能:背景色、字体颜色、文本、停留时间

<template>
<div class&#61;"toast-box" ><p class&#61;"toast-value" :style&#61;"{background: background, color: color}">{{ value }}</p>
</div>
</template>
<script>import { defineComponent } from &#39;vue&#39;export default defineComponent({name: &#39;Toast&#39;,props: {value: {type: String,default: &#39;&#39;},duration: {type: Number,default: 3000},background: {type: String,default: &#39;#000&#39;},color: {type: String,default: &#39;#fff&#39;}}})
</script><style>
.toast-box {position: fixed;width: 100vw;height: 100vh;top: 0;left: 0;display: flex;align-items: center;justify-content: center;z-index: 1000;
}.toast-value {max-width: 100px;background: rgb(8, 8, 8);padding: 8px 10px;border-radius: 4px;text-align: center;display: inline-block;animation: anim 0.5s;}&#64;keyframes anim { 0% {opacity: 0;}100%{opacity:1;}}.toast-value.remove{animation: remove 0.5s;}&#64;keyframes remove { 0% {opacity: 1;}100%{opacity:0;}}
</style>

2、index.js 导出Toast方法


  • 创建
    首先使用createVNode方法创建一个vNode独享
    使用render方法转换成真实dom
    添加到body

  • 销毁
    首先添加一个淡入淡出效果
    使用render将真实设置为null
    移除创建的dom

以下代码为TS写法&#xff0c;不支持部分去掉代码即可

import { createVNode, render } from &#39;vue&#39;
import toastTemplate from &#39;./index.vue&#39;
export interface IProps {value?: string;duration?: number;background?: string;color?: string;
}
const defaultOpt &#61; { // 创建默认参数duration: 3000
}export interface ResultParams {destory?: () &#61;> void;
}
// eslint-disable-next-line &#64;typescript-eslint/explicit-module-boundary-types
const Toast &#61; (options: IProps):ResultParams &#61;> {const container &#61; document.createElement(&#39;div&#39;)const opt &#61; {...defaultOpt,...options}const vm &#61; createVNode(toastTemplate, opt) // 创建vNoderender(vm, container)document.body.appendChild(container) // 添加到body上const destory &#61; ()&#61;> {const dom &#61; vm.el as HTMLDivElementif(dom.querySelector(&#39;.toast-value&#39;)) {dom.querySelector(&#39;.toast-value&#39;)?.classList.add(&#39;remove&#39;) // 销毁时添加淡入淡出效果const t &#61; setTimeout(() &#61;> { // 淡入淡出效果之后删除dom节点render(null, container)document.body.removeChild(container)clearTimeout(t)},500);} }if(opt.duration) { // 如果传入的值为0可以持续保留在页面&#xff0c;需要手动销毁const timer &#61; setTimeout(()&#61;> {destory()clearTimeout(timer)}, opt.duration)}return {destory}
}
export default Toast

3、main.js插件全局引入

import Toast from &#39;&#64;/components/Toast/index&#39;app.config.globalProperties.$toast &#61; Toast;
// app.use(Toast) 用use来全局载入会导致我们不能使用this的地方不太好调用。

4、使用

this.$toast({value: &#39;toast&#39;,duration: 0, // 如果大于0则不必使用destory方法background: &#39;#000&#39;,color: &#39;#fff&#39;
})
setTimeout(() &#61;> {this.$toast.destory && this.$toast.destory()
}, 2000)

setup内使用

import { getCurrentInstance} from &#39;vue&#39;setup() {const { proxy } &#61; getCurrentInstance();const showToastEvent &#61; () &#61;> {proxy.$toast({value: &#39;toast&#39;,duration: 1000,background: &#39;#000&#39;,color: &#39;#fff&#39;})}return {showToastEvent,}
}

loading

<template><div class&#61;"loading">加载中...</div>
</template><script>export default {name: "loading",}
</script><style scoped>.loading {position: fixed;left: 50%;top: 50%;background-color: rgba(0, 0, 0, 0.2);color: white;transform: translate(-50%, -50%);border-radius: 4px;padding: 8px 10px;z-index: 1000;}
</style>

import { createApp } from "vue"import Loading from &#39;./loading.vue&#39;export default {instance: null,parent: null,times: 0, // 为了保证多个同时loading的时候&#xff0c;只显示一个&#xff0c;并且需要全部close之后才消失open() {if (this.times &#61;&#61; 0) {this.instance &#61; createApp(Loading)this.parent &#61; document.createElement("div")let appDom &#61; document.getElementById(&#39;app&#39;)appDom.appendChild(this.parent)this.instance.mount(this.parent)}this.times &#43;&#43;},close() {this.times --if (this.times <&#61; 0) {this.times &#61; 0let appDom &#61; document.getElementById(&#39;app&#39;)this.instance.unmount(this.parent)appDom.removeChild(this.parent)}}
};

import loading from &#39;&#64;/components/loading/index&#39;
app.config.globalProperties.$loading &#61; loading;

推荐阅读
  • 本文介绍了如何使用vue-awesome-swiper组件,包括在main.js中引入和使用swiper和swiperSlide组件,以及设置options和ref属性。同时还介绍了如何在模板中使用swiper和swiperSlide组件,并展示了如何通过循环渲染swipes数组中的数据,并使用picUrl属性显示图片。最后还介绍了如何添加分页器。 ... [详细]
  • 我试图在Angular2应用程序中使用元素调整大小检测器库(https:github.comwnrelement-resize-detector).根据我有限的JS模块知识,该库似 ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
  • 本文讨论了将HashRouter改为Router后,页面全部变为空白页且没有报错的问题。作者提到了在实际部署中需要在服务端进行配置以避免刷新404的问题,并分享了route/index.js中hash模式的配置。文章还提到了在vueJs项目中遇到过类似的问题。 ... [详细]
  • 小编给大家分享一下TypeScript2.7有什么改进,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收 ... [详细]
  • 深入理解TypeScript认识TypeScript&配置详解
    为什么使用TypeScript?最初使用TypeScript开发项目的时候,觉得很繁琐,定义每一个变量还要加上类型,尤其是对象形式的,几乎每一个用到的都要定一个接口,还要为每一 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了2015年九月八日的js学习总结及相关知识点,包括参考书《javaScript Dom编程的艺术》、js简史、Dom、DHTML、解释型程序设计和编译型程序设计等内容。同时还提到了最佳实践是将标签放到HTML文档的最后,并且对语句和注释的使用进行了说明。 ... [详细]
  • Vue3中setup函数的参数props和context配置详解
    本文详细介绍了Vue3中setup函数的参数props和context的配置方法,包括props的接收和配置声明,以及未通过props进行接收配置时的输出值。同时还介绍了父组件传递给子组件的值和模板的相关内容。阅读本文后,读者将对Vue3中setup函数的参数props和context的配置有更深入的理解。 ... [详细]
  • Python已成为全球最受欢迎的编程语言之一,然而Python程序的安全运行存在一定的风险。本文介绍了Python程序安全运行需要满足的三个条件,即系统路径上的每个条目都处于安全的位置、"主脚本"所在的目录始终位于系统路径中、若python命令使用-c和-m选项,调用程序的目录也必须是安全的。同时,文章还提出了一些预防措施,如避免将下载文件夹作为当前工作目录、使用pip所在路径而不是直接使用python命令等。对于初学Python的读者来说,这些内容将有所帮助。 ... [详细]
  • 本文介绍了自学Vue的第01天的内容,包括学习目标、学习资料的收集和学习方法的选择。作者解释了为什么要学习Vue以及选择Vue的原因,包括完善的中文文档、较低的学习曲线、使用人数众多等。作者还列举了自己选择的学习资料,包括全新vue2.5核心技术全方位讲解+实战精讲教程、全新vue2.5项目实战全家桶单页面仿京东电商等。最后,作者提出了学习方法,包括简单的入门课程和实战课程。 ... [详细]
  • Vue基础一、什么是Vue1.1概念Vue(读音vjuː,类似于view)是一套用于构建用户界面的渐进式JavaScript框架,与其它大型框架不 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有