您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue+toast弹窗组件使用案例详解

2024/3/2 2:34:33发布13次查看
这次给大家带来vue+toast弹窗组件使用案例详解,vue+toast弹窗组件使用的注意事项有哪些,下面就是实战案例,一起来看一下。
相信普通的vue组件大家都会写, 定义 -> 引入 -> 注册 -> 使用 ,行云流水,一气呵成,但是如果我们今天是要自定义一个弹窗组件呢?
首先,我们来分析一下弹窗组件的特性(需求):
0. 轻量 --一个组件小于 1kib (实际打包完不到0.8k)
1.一般都是多处使用 --需要解决每个页面重复引用+注册
1.一般都是跟js交互的 --无需 在 <template> 里面写 <toast :show="true" text="弹窗消息"></toast>
今天,我们就抱着上面2个需求点,来实现一个基于vue的toast弹窗组件,下图是最终完成的效果图.
一. 先写一个普通的vue组件
文件位置 /src/toast/toast.vue
<template>  <p class="wrap">我是弹窗</p> </template> <style scoped>  .wrap{  position: fixed;  left: 50%;  top:50%;  background: rgba(0,0,0,.35);  padding: 10px;  border-radius: 5px;  transform: translate(-50%,-50%);  color:#fff;  } </style>
二. 在我们需要使用的页面引入组件,方便看效果和错误
<template>  <p id="app">  <toast></toast>  </p> </template> <script>  import toast from './toast/toast'  export default {  components: {toast},  } </script>
三. 实现动态加载组件
可以看到,已经显示出一个静态的弹出层了,接下来我们就来看看如何实现动态弹出.
我们先在 /src/toast/ 目录下面,新建一个 index.js , 然后在index.js里面,敲入以下代码(由于该代码耦合比较严重,所以就不拆开一行一行讲解了,改成行内注释)
文件位置 /src/toast/index.js
import vue from 'vue' // 这里就是我们刚刚创建的那个静态组件 import toastcomponent from './toast.vue' // 返回一个 扩展实例构造器 const toastconstructor = vue.extend(toastcomponent) // 定义弹出组件的函数 接收2个参数, 要显示的文本 和 显示时间 function showtoast(text, duration = 2000) {  // 实例化一个 toast.vue  const toastdom = new toastconstructor({  el: document.createelement('p'),  data() {  return {  text:text,  show:true  }  }  })  // 把 实例化的 toast.vue 添加到 body 里  document.body.appendchild(toastdom.$el)  // 过了 duration 时间后隐藏  settimeout(() => {toastdom.show = false} ,duration) } // 注册为全局组件的函数 function registrytoast() {  // 将组件注册到 vue 的 原型链里去,  // 这样就可以在所有 vue 的实例里面使用 this.$toast()  vue.prototype.$toast = showtoast }
export default registrytoast
附一个传送门vue.extend 官方文档
四. 试用
到这里,我们已经初步完成了一个可以全局注册和动态加载的toast组件,接下来我们来试用一下看看
在vue的入口文件(脚手架生成的话是 ./src/main.js ) 注册一下组件
文件位置 /src/main.js
import toastregistry from './toast/index' // 这里也可以直接执行 toastregistry() vue.use(toastregistry) 我们稍微修改一下使用方式,把 第二步 的引用静态组件的代码,改成如下 <template>  <p id="app">  <input type="button" value="显示弹窗" @click="showtoast">  </p> </template> <script>  export default {  methods: {  showtoast () {  this.$toast('我是弹出消息')  }  }  } </script>
可以看到,我们已经 不需要 在页面里面 引入 跟 注册 组件,就可以直接使用 this.$toast() 了.
五. 优化
现在我们已经初步实现了一个弹窗.不过离成功还差一点点,缺少一个动画,现在的弹出和隐藏都很生硬.
我们再对 toast/index.js 里的 showtoast 函数稍微做一下修改(有注释的地方是有改动的)
文件位置 /src/toast/index.js
function showtoast(text, duration = 2000) {  const toastdom = new toastconstructor({  el: document.createelement('p'),  data() {  return {  text:text,  showwrap:true, // 是否显示组件  showcontent:true // 作用:在隐藏组件之前,显示隐藏动画  }  }  })  document.body.appendchild(toastdom.$el)  // 提前 250ms 执行淡出动画(因为我们再css里面设置的隐藏动画持续是250ms)  settimeout(() => {toastdom.showcontent = false} ,duration - 1250)  // 过了 duration 时间后隐藏整个组件  settimeout(() => {toastdom.showwrap = false} ,duration) }
然后,再修改一下toast.vue的样式
文件位置 /src/toast/toast.vue
<template>  <p class="wrap" v-if="showwrap" :class="showcontent ?'fadein':'fadeout'">{{text}}</p> </template> <style scoped>  .wrap{  position: fixed;  left: 50%;  top:50%;  background: rgba(0,0,0,.35);  padding: 10px;  border-radius: 5px;  transform: translate(-50%,-50%);  color:#fff;  }  .fadein {  animation: animate_in 0.25s;  }  .fadeout {  animation: animate_out 0.25s;  opacity: 0;  }  @keyframes animate_in {  0% {  opacity: 0;  }  100%{  opacity: 1;  }  }  @keyframes animate_out {  0% {  opacity: 1;  }  100%{  opacity: 0;  }  } </style>
大功告成,一个toast组件初步完成
总结
vue.extend 函数可以生成一个 组件构造器 可以用这个函数构造出一个 vue组件实例
可以用 document.body.appendchild() 动态的把组件加到 body里面去
vue.prototype.$toast = showtoast 可以在全局注册组件
显示动画比较简单,隐藏动画必须要在隐藏之前预留足够的动画执行时间
本文源码地址 在这里
以上都不重要,重要的是 给本文来个 star
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
jquery元素选择器使用案例详解
vue动态绑定组件子父组件多表单验证实现步骤详解
以上就是vue+toast弹窗组件使用案例详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product