欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue從零實現(xiàn)一個消息通知組件的方法詳解

 更新時間:2020年03月16日 08:38:46   作者:wangliang_001  
這篇文章主要介紹了vue從零實現(xiàn)一個消息通知組件的方法,結(jié)合實例形式分析了vue實現(xiàn)消息通知組件的具體原理、實現(xiàn)步驟、與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了vue從零實現(xiàn)一個消息通知組件的方法。分享給大家供大家參考,具體如下:

利用vue從零實現(xiàn)一個消息通知組件

平時,我們肯定用過類似element-ui,antd等一些UI框架,感受它們帶給我們的便利。但當我們的需求或者設(shè)計這些框架內(nèi)置的相差太大,用起來,就會覺得特別別扭,這時候,就有必要自己來重新造輪子。

重新造輪子,有幾個好處,1.所有代碼都是服務(wù)你的業(yè)務(wù),沒有太多用不上的東西。2.代碼是由自己維護,而不是第三方,方便維護。3.提升自己的視野,讓自己站在更高的角度來看問題。

好了,那話不多說,開始我們的組件開發(fā)吧!

文件目錄的組件

工欲善其事,必先利其器,要想實現(xiàn)一個組件,一個好的目錄結(jié)構(gòu),即可以劃分職責,不同模塊處理不同的邏輯!

我的目錄結(jié)果是這樣的:
目錄結(jié)構(gòu)

接下來,我們依次對notification.vue, notify.js, index.js三個文件作介紹。

notification.vue

notification.vue是一個負責消息通知組件的視覺呈現(xiàn),里面的邏輯很簡單。

<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: '關(guān)閉'
  }
 }
}
</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中的內(nèi)容插入dom節(jié)點中去
 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組件實現(xiàn)注冊,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.js程序設(shè)計有所幫助。

相關(guān)文章

  • Vue Elenent實現(xiàn)表格相同數(shù)據(jù)列合并

    Vue Elenent實現(xiàn)表格相同數(shù)據(jù)列合并

    這篇文章主要為大家詳細介紹了Vue Elenent實現(xiàn)表格相同數(shù)據(jù)列合并,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • vue內(nèi)置動態(tài)組件component使用示例小結(jié)

    vue內(nèi)置動態(tài)組件component使用示例小結(jié)

    component是vue內(nèi)置組件,主要作用為動態(tài)渲染組件,這篇文章主要介紹了vue內(nèi)置動態(tài)組件component使用示例小結(jié),需要的朋友可以參考下
    2024-03-03
  • vue如何導(dǎo)出json數(shù)據(jù)為excel表格并保存到本地

    vue如何導(dǎo)出json數(shù)據(jù)為excel表格并保存到本地

    這篇文章主要介紹了vue如何導(dǎo)出json數(shù)據(jù)為excel表格并保存到本地問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue 之 css module的使用方法

    vue 之 css module的使用方法

    這篇文章主要介紹了vue 之 css module的使用方法,css module目的為所有類名重新生成類名,有效避開了css權(quán)重和類名重復(fù)的問題,非常具有實用價值,需要的朋友可以參考下
    2018-12-12
  • vue開發(fā)拖拽進度條滑動組件

    vue開發(fā)拖拽進度條滑動組件

    這篇文章主要為大家詳細介紹了vue開發(fā)拖拽進度條滑動組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Vue自定義事件(詳解)

    Vue自定義事件(詳解)

    下面小編就為大家?guī)硪黄猇ue自定義事件(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • vue?parseHTML?函數(shù)拿到返回值后的處理源碼解析

    vue?parseHTML?函數(shù)拿到返回值后的處理源碼解析

    這篇文章主要為大家介紹了vue?parseHTML?函數(shù)拿到返回值后的處理源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • vue常見路由跳轉(zhuǎn)的幾種方式小結(jié)

    vue常見路由跳轉(zhuǎn)的幾種方式小結(jié)

    本文主要介紹了常見路由跳轉(zhuǎn)的幾種方式,主要介紹了四種常見方式,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • element中el-cascader級聯(lián)選擇器只有最后一級可以多選

    element中el-cascader級聯(lián)選擇器只有最后一級可以多選

    本文主要介紹了element中el-cascader級聯(lián)選擇器只有最后一級可以多選,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • vue3使用vue-cli引入windicss報錯Can‘t resolve windi.css問題

    vue3使用vue-cli引入windicss報錯Can‘t resolve windi.css問題

    這篇文章主要介紹了vue3使用vue-cli引入windicss報錯Can‘t resolve windi.css問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論