vue从零实现一个消息通知组件的方法详解


Posted in Javascript onMarch 16, 2020

本文实例讲述了vue从零实现一个消息通知组件的方法。分享给大家供大家参考,具体如下:

利用vue从零实现一个消息通知组件

平时,我们肯定用过类似element-ui,antd等一些UI框架,感受它们带给我们的便利。但当我们的需求或者设计这些框架内置的相差太大,用起来,就会觉得特别别扭,这时候,就有必要自己来重新造轮子。

重新造轮子,有几个好处,1.所有代码都是服务你的业务,没有太多用不上的东西。2.代码是由自己维护,而不是第三方,方便维护。3.提升自己的视野,让自己站在更高的角度来看问题。

好了,那话不多说,开始我们的组件开发吧!

文件目录的组件

工欲善其事,必先利其器,要想实现一个组件,一个好的目录结构,即可以划分职责,不同模块处理不同的逻辑!

我的目录结果是这样的:
vue从零实现一个消息通知组件的方法详解

接下来,我们依次对notification.vue, notify.js, index.js三个文件作介绍。

notification.vue

notification.vue是一个负责消息通知组件的视觉呈现,里面的逻辑很简单。

<template>
 <transition name="fade" @after-enter="handleAfterEnter">
  <div class="notification" :style="style" v-show="visible">
   <span class="notification__content">
    {{content}}
   </span>
   <span class="notification__btn" @click="handleClose">{{btn}}</span>
  </div>
 </transition>
</template>
<script>
export default {
 name: 'Notification',
 props: {
  content: {
   type: String,
   required: true
  },
  btn: {
   type: String,
   default: '关闭'
  }
 }
}
</script>
<style lang="less" scoped>
.fade-enter-active, .fade-leave-active{
 transition: opacity 1s;
}
.fade-enter, .fade-leave-to{
 opacity: 0;
}
.notification{
 display: flex;
 background-color: #303030;
 color: rgba(255, 255, 255, 1);
 align-items: center;
 padding: 20px;
 position: fixed;
 min-width: 280px;
 box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.3);
 flex-wrap: wrap;
 transition: all 0.3s;
 &__content{
  padding: 0;
 }
 &__btn{
  color: #ff4081;
  padding-left: 24px;
  margin-left: auto;
  cursor: pointer;
 }
}
</style>

notify.js

notify.js是一个处理消息通知组件的逻辑部分,其主要作用是暴露一个notify的方法出去。代码如下:

import Vue from 'vue'
import Notification from './notification'

const NotificationConstructor = Vue.extend(Notification)

const instances = []
let seed = 1
const removeInstance = (instance) => {
 if (!instance) return
 const len = instances.length
 const index = instances.findIndex(ins => instance.id === ins.id)

 instances.splice(index, 1)

 if (len <= 1) return
 const removeHeight = instance.height
 for (let i = index; i < len - 1; i++) {
  instances[i].verticalOffset = parseInt(instances[i].verticalOffset) - removeHeight - 16
 }
}
const notify = (options = {}) => {
 if (Vue.prototype.$isServer) return
 // 获取vue实例
 let instance = new NotificationConstructor({
  propsData: options,
  data() {
   return {
    verticalOffset: 0,
    timer: null,
    visible: false,
    height: 0
   }
  },
  computed: {
   style() {
    return {
     position: 'fixed',
     right: '20px',
     bottom: `${this.verticalOffset}px`
    }
   }
  },
  mounted() {
   this.createTimer()
   this.$el.addEventListener('mouseenter', () => {
    if (this.timer) {
     this.clearTimer(this.timer)
    }
   })
   this.$el.addEventListener('mouseleave', () => {
    if (this.timer) {
     this.clearTimer(this.timer)
    }
    this.createTimer()
   })
  },
  updated() {
   this.height = this.$el.offsetHeight
  },
  beforeDestroy() {
   this.clearTimer()
  },
  methods: {
   createTimer() {
    this.timer = setTimeout(() => {
     this.visible = false
     document.body.removeChild(this.$el)
     removeInstance(this)
     this.$destroy()
    }, options.timeout || 3000)
   },
   clearTimer() {
    if (this.timer) {
     clearTimeout(this.timer)
    }
   },
   handleClose() {
    this.visible = false
    document.body.removeChild(this.$el)
    removeInstance(this)
    this.$destroy(true)
   },
   handleAfterEnter() {
    // eslint-disable-next-line no-debugger
    this.height = this.$el.offsetHeight
   }
  }
 })

 const id = `notification_${seed++}`
 instance.id = id
 // 生成vue中的$el
 instance = instance.$mount()
 // 将$el中的内容插入dom节点中去
 document.body.appendChild(instance.$el)
 instance.visible = true

 // eslint-disable-next-line no-unused-vars
 let verticalOffset = 0

 instances.forEach(item => {
  verticalOffset += item.$el.offsetHeight + 16
 })

 verticalOffset += 16
 instance.verticalOffset = verticalOffset

 instances.push(instance)

 return instance
}

export default notify

index.js

index.js主要是对notification.vue组件实现注册,notify方法的挂载。代码如下:

import Notification from './notification'
import notify from './notify'

export default (Vue) => {
 Vue.component(Notification.name, Notification)
 Vue.prototype.$notify = notify
}

在main.js引入

import Notification from './components/notification'
Vue.use(Notification)

使用

this.$notify({
 content: 'Hello'
})

效果

vue从零实现一个消息通知组件的方法详解

希望本文所述对大家vue.js程序设计有所帮助。

Javascript 相关文章推荐
JavaScript中yield实用简洁实现方式
Jun 12 Javascript
原生js 秒表实现代码
Jul 24 Javascript
一个封装js代码-----展开收起效果示例
Jul 03 Javascript
jQuery实现的一个tab切换效果内部还嵌有切换
Aug 10 Javascript
jquery删除指定子元素代码实例
Jan 13 Javascript
Javascript实现的简单右键菜单类
Sep 23 Javascript
js检测iframe是否加载完成的方法
Nov 26 Javascript
JavaScript中Function函数与Object对象的关系
Dec 17 Javascript
XMLHttpRequest Level 2 使用指南
Aug 26 Javascript
js canvas画布实现高斯模糊效果
Nov 27 Javascript
Vue解析剪切板图片并实现发送功能
Feb 04 Javascript
微信小程序scroll-view不能左右滑动问题的解决方法
Jul 09 Javascript
JavaScript实现动态留言板
Mar 16 #Javascript
vue中的双向数据绑定原理与常见操作技巧详解
Mar 16 #Javascript
js+canvas实现纸牌游戏
Mar 16 #Javascript
微信小程序利用button控制条件标签的变量问题
Mar 15 #Javascript
JS apply用法总结和使用场景实例分析
Mar 14 #Javascript
javascript事件循环event loop的简单模型解释与应用分析
Mar 14 #Javascript
原生js实现ajax请求和JSONP跨域请求操作示例
Mar 14 #Javascript
You might like
PHP字符串中特殊符号的过滤方法介绍
2014/02/18 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
2014/05/23 PHP
PHP命名空间(namespace)的动态访问及使用技巧
2014/08/18 PHP
PHP使用strrev翻转中文乱码问题的解决方法
2017/01/13 PHP
POST一个JSON格式的数据给Restful服务实例详解
2017/04/07 PHP
phpinfo的知识点总结
2019/10/10 PHP
PHP设计模式之迭代器模式Iterator实例分析【对象行为型】
2020/04/26 PHP
JS与框架页的操作代码
2010/01/17 Javascript
动态加载图片路径 保持JavaScript控件的相对独立性
2010/09/03 Javascript
js使用for循环查询数组中是否存在某个值
2014/08/12 Javascript
javascript实现图像循环明暗变化的方法
2015/02/25 Javascript
Extjs4.0 ComboBox如何实现三级联动
2016/05/11 Javascript
详解jQuery中的事件
2016/12/14 Javascript
利用js查找数组中指定元素并返回该元素的所有索引示例
2017/03/29 Javascript
Vue应用部署到服务器的正确方式
2017/07/15 Javascript
详解VUE Element-UI多级菜单动态渲染的组件
2019/04/25 Javascript
JS实现简单随机3D骰子
2019/10/24 Javascript
Python中for循环和while循环的基本使用方法
2015/08/21 Python
Python脚本完成post接口测试的实例
2018/12/17 Python
Python面向对象之类的封装操作示例
2019/06/08 Python
Python进度条的制作代码实例
2019/08/31 Python
详解HTML5中垂直上下居中的解决方案
2017/12/20 HTML / CSS
HTML5验证以及日期显示的实现详解
2013/07/05 HTML / CSS
华三通信H3C面试题
2015/05/15 面试题
大学团支书的自我评价分享
2013/12/14 职场文书
培训讲师邀请函
2014/01/10 职场文书
会计学专业学生的求职信范文
2014/01/27 职场文书
秘书英文求职信
2014/04/16 职场文书
授权委托书协议书
2014/10/16 职场文书
群众路线教育实践活动学习心得体会
2014/10/30 职场文书
2014年客户经理工作总结
2014/11/20 职场文书
教师节慰问信
2015/02/15 职场文书
同学聚会通知短信
2015/04/20 职场文书
爸爸的三轮车观后感
2015/06/16 职场文书
学生检讨书范文
2019/06/24 职场文书
《艾尔登法环》Boss腐烂树灵很有可能是《黑暗之魂3》的一个废案
2022/04/11 其他游戏