最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

文件目錄的組件

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

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

接下來(lái),我們依次對(duì)notification.vue, notify.js, index.js三個(gè)文件作介紹。

notification.vue

notification.vue是一個(gè)負(fù)責(zé)消息通知組件的視覺(jué)呈現(xiàn),里面的邏輯很簡(jiǎ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是一個(gè)處理消息通知組件的邏輯部分,其主要作用是暴露一個(gè)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實(shí)例
 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é)點(diǎn)中去
 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主要是對(duì)notification.vue組件實(shí)現(xiàn)注冊(cè),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'
})

效果

效果

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序

    vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序

    本文主要介紹了vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Vue之封裝公用變量以及實(shí)現(xiàn)方式

    Vue之封裝公用變量以及實(shí)現(xiàn)方式

    這篇文章主要介紹了Vue之封裝公用變量以及實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • vue使用file-saver本地文件導(dǎo)出功能

    vue使用file-saver本地文件導(dǎo)出功能

    這篇文章主要介紹了vue使用file-saver本地文件導(dǎo)出,大家需要安裝xlsx和file-saver,然后創(chuàng)建localExports.js文件,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2022-01-01
  • 一文教你解決Vue所有報(bào)錯(cuò)

    一文教你解決Vue所有報(bào)錯(cuò)

    Vue是一個(gè)流行的前端框架,許多web開(kāi)發(fā)人員使用Vue來(lái)構(gòu)建他們的應(yīng)用程序。然而,正如任何其他框架一樣,Vue也可能會(huì)發(fā)生錯(cuò)誤。在這篇技術(shù)文章中,我們將探討Vue常見(jiàn)的報(bào)錯(cuò)以及如何解決它們
    2023-03-03
  • vue3.0中setup使用(兩種用法)

    vue3.0中setup使用(兩種用法)

    這篇文章主要介紹了vue3.0中setup使用,本文通過(guò)兩種用法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 在Vue環(huán)境下利用worker運(yùn)行interval計(jì)時(shí)器的步驟

    在Vue環(huán)境下利用worker運(yùn)行interval計(jì)時(shí)器的步驟

    這篇文章主要介紹了在Vue環(huán)境下利用worker運(yùn)行interval計(jì)時(shí)器的步驟,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 在vue中使用echarts的方法以及可能遇到的問(wèn)題

    在vue中使用echarts的方法以及可能遇到的問(wèn)題

    Echarts是一個(gè)與框架無(wú)關(guān)的JS圖表庫(kù),但是它基于Js,這樣很多框架都能使用它,下面這篇文章主要給大家介紹了關(guān)于在vue中使用echarts的方法以及可能遇到的問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • vue解決使用$http獲取數(shù)據(jù)時(shí)報(bào)錯(cuò)的問(wèn)題

    vue解決使用$http獲取數(shù)據(jù)時(shí)報(bào)錯(cuò)的問(wèn)題

    今天小編就為大家分享一篇vue解決使用$http獲取數(shù)據(jù)時(shí)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • vue-cli如何修改打包項(xiàng)目結(jié)構(gòu)及前綴

    vue-cli如何修改打包項(xiàng)目結(jié)構(gòu)及前綴

    這篇文章主要介紹了vue-cli如何修改打包項(xiàng)目結(jié)構(gòu)及前綴問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • vue的toast彈窗組件實(shí)例詳解

    vue的toast彈窗組件實(shí)例詳解

    本文通過(guò)實(shí)例給大家講解了vue的toast彈窗組件功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-05-05

最新評(píng)論

合肥市| 安多县| 合作市| 景洪市| 临朐县| 九江市| 濉溪县| 景东| 南部县| 资阳市| 云梦县| 于都县| 克拉玛依市| 阿拉善盟| 博客| 潮州市| 肃宁县| 罗山县| 福海县| 敦化市| 隆子县| 特克斯县| 新巴尔虎左旗| 梧州市| 江油市| 台南县| 汽车| 长宁区| 婺源县| 永定县| 临泽县| 车致| 华池县| 宁乡县| 普格县| 霍林郭勒市| 永顺县| 杨浦区| 沙坪坝区| 临桂县| 沾化县|