国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

vue從零實現一個消息通知組件的方法詳解

瀏覽:7日期:2023-01-31 09:41:55

本文實例講述了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 : v-show='visible'> <span class='notification__content'> {{content}} </span> <span @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 = 1const 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 notifyindex.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程序設計有所幫助。

標簽: Vue
相關文章:
主站蜘蛛池模板: 日韩在线视精品在亚洲 | 免费一级欧美大片视频在线 | 亚洲精品精品一区 | 97在线视频免费观看 | 国产成人一区二区三区高清 | 久久精品一区二区三区不卡牛牛 | 精品国产一区二区二三区在线观看 | 有码 在线 | 亚洲图片在线视频 | 欧美另类丝袜 | 91精品91 | 一本色道久久99一综合 | 久久亚洲精品成人综合 | 99视频精品免视3 | 成人免费在线视频网 | 在线观看精品国内福利视频 | 国产免费久久精品99 | 亚洲国产天堂久久综合网站 | 欧美激情亚洲一区中文字幕 | 亚洲精品欧美 | 久久久久欧美精品网站 | 国产女王s调视频vk 国产女王vk | 成人午夜视频在线播放 | 美女拍拍拍爽爽爽爽爽爽 | 国产精品免费看 | 久久精品国产欧美日韩99热 | 国产短裙黑色丝袜在线观看下 | 黄色a网站| 99re9精品视频在线 | 亚欧成人毛片一区二区三区四区 | 成人国产精品一级毛片天堂 | 午夜性福| 国产精品一区二区综合 | 男女做性免费视频软件 | 国内主播福利视频在线观看 | 美女被免费网站在线视频软件 | 俄罗斯一级成人毛片 | 一级做a爰片欧美一区 | 高清一区二区在线观看 | 国产欧美成人免费观看 | 3至13呦女毛片|