相信普通的vue组件你们都会写,定义 -> 引入 -> 注册 -> 使用
,行云流水,一鼓作气,可是若是咱们今天是要自定义一个弹窗组件呢?css
首先,咱们来分析一下弹窗组件的特性(需求): 0. 轻量 --一个组件小于 1Kib (实际打包完不到0.8k)vue
<template>
里面写 <toast :show="true" text="弹窗消息"></toast>
今天,咱们就抱着上面2个需求点,来实现一个基于vue的toast弹窗组件,下图是最终完成的效果图.git
文件位置 /src/toast/toast.vue
github
<template>
<div class="wrap">我是弹窗</div>
</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>
<div id="app">
<toast></toast>
</div>
</template>
<script>
import toast from './toast/toast'
export default {
components: {toast},
}
</script>
复制代码
能够看到,已经显示出一个静态的弹出层了,接下来咱们就来看看如何实现动态弹出.api
咱们先在 /src/toast/
目录下面,新建一个index.js
, 而后在index.js里面,敲入如下代码(因为该代码耦合比较严重,因此就不拆开一行一行讲解了,改为行内注释)bash
文件位置 /src/toast/index.js
app
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('div'),
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组件,接下来咱们来试用一下看看优化
./src/main.js
) 注册一下组件文件位置 /src/main.js
动画
import toastRegistry from './toast/index'
// 这里也能够直接执行 toastRegistry()
Vue.use(toastRegistry)
复制代码
第二步
的引用静态组件的代码,改为以下<template>
<div id="app">
<input type="button" value="显示弹窗" @click="showToast">
</div>
</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('div'),
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>
<div class="wrap" v-if="showWrap" :class="showContent ?'fadein':'fadeout'">{{text}}</div>
</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组件初步完成