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

vue通知提醒消息舉例詳解

 更新時間:2023年03月24日 08:49:25   作者:混世大魔王955  
在項目開發(fā)過程中,可能需要實現(xiàn)以下場景,未讀消息提示、報警信息、消息通知等,下面這篇文章主要給大家介紹了關(guān)于vue通知提醒消息的相關(guān)資料,需要的朋友可以參考下

前言

最近有個項目需求就是在客戶端的右上角要實時展示提醒消息,下面來看下簡單的實現(xiàn)步驟

一、Notification

這是基于懸浮出現(xiàn)在頁面角落,顯示全局的通知提醒消息。這個elmennt-ui組件可以實現(xiàn)我們上面的功能。

二、Notification引用

1.全局引用 

element 為 Vue.prototype 添加了全局方法 $notify。因此在 vue instance 中可以采用本頁面中的方式調(diào)用 Notification。

2.單獨引用

import { Notification } from 'element-ui';

此時調(diào)用方法為 Notification(options)。我們也為每個 type 定義了各自的方法,如 Notification.success(options)。并且可以調(diào)用 Notification.closeAll() 手動關(guān)閉所有實例。

三、參數(shù)說明

四、簡單案例 

右上角就會彈出我們寫的html代碼段是不是特別簡單

<template>
  <el-button
    plain
    @click="open">
    使用 HTML 片段
  </el-button>
</template>
 
<script>
  export default {
    methods: {
      open() {
        this.$notify({
          title: 'HTML 片段',
          dangerouslyUseHTMLString: true,
          message: '<strong>這是 <i>HTML</i> 片段</strong>'
        });
      }
    }
  }
</script>

五、項目實戰(zhàn)

這里大概說一下我的流程,我這里需要建立Websocket連接,服務(wù)器實時推送信息給客戶端在右上角展示,這里需要用到Websocket以及本章學(xué)的通知。Websocket在前一章有講。案例僅供參考。

1、定義全局Notification。

/* 全局Notification */
	Vue.prototype.$baseNotify = (message, title, type, position) => {
		Notification({
			title: title,
			message: message,
			position: position || 'top-right',
			type: type || 'success',
			duration: messageDuration,
		})
	}

2、Websocket實時接收通知。

initWebSocket() {
        const token = getAccessToken()
 
        const wsurl = `${this.troubelUrl}?code=trouble&token=${token}`
        this.twebsock = new WebSocket(wsurl)
        this.twebsock.onmessage = this.websocketonmessage
        this.twebsock.onopen = this.websocketonopen
        this.twebsock.onerror = this.websocketonerror
        this.twebsock.onclose = this.websocketclose
      },
      websocketonopen() {
        //webscoket定時心跳
        this.troubleTimer = setInterval(() => {
          let pageUrl = window.location.hash
          if (pageUrl !== '' && pageUrl !== '#/login') {
            this.websocketsend('heartbeat')
          }
        }, 50000)
        console.log('數(shù)據(jù)發(fā)送...')
      },
      websocketonerror(e) {
        //連接建立失敗重連
        setTimeout(() => {
          this.initWebSocket()
        }, 10000)
        console.log('故障連接出錯~')
      },
      websocketonmessage(evt) {
        var monitorData = evt.data
        monitorData = JSON.parse(monitorData)
        this.switchOther(this.troublePush, monitorData)
      },
      //根據(jù)數(shù)據(jù)判斷進行彈框(緊急呼叫,長時間關(guān)人)
      switchOther(switchValue, monitorData) {
        if (switchValue === true || switchValue === 'true') {
            this.handleOpen(monitorData)
        }
      },
      websocketsend(data) {
        this.twebsock.send(data)
      },
      websocketclose(e) {
        if (this.twebsock == null) {
          return
        }
        this.twebsock.close()
        this.twebsock = null
        clearInterval(this.troubleTimer)
        console.log('故障推送關(guān)閉~')
      },

3、消息通知

      //monitorItem取的前面Websocket返回回來的值
      handleOpen(monitorItem) {
        this.openDialogflase = true
        const h = this.$createElement
        let notify = this.$notify({
          title: monitorItem.troubleType,
          message: h('p', null, [
            h(
              'span',
              {
                style: {
                  display: 'inline-block',
                  margin: '0 0 10px 0',
                },
              },
              `${monitorItem.projectName}-${monitorItem.useCode}`
            ),
            h(
              'p',
              {
                style: {
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'space-between',
                  margin: '0 0 5px 0',
                },
              },
              [
                h('span', null, monitorItem.duration),
                h(
                  'span',
                  {
                    style: {
                      color: '#efefef',
                    },
                  },
                  monitorItem.fromType
                ),
              ]
            ),
            h('p', null, monitorItem.address),
            h(
              'button',
              {
                style: {
                  padding: '5px 20px',
                  fontSize: '14px',
                  borderRadius: '4px',
                  color: '#fff',
                  background: '#ff575a',
                  border: 'none',
                  margin: '10px 10px 0 0',
                  display: 'inline-block',
                },
                on: {
                  click: this.clickBtn.bind(this, monitorItem),
                },
              },
              '查看詳情'
            ),
            h(
              'button',
              {
                style: {
                  padding: '5px 20px',
                  fontSize: '14px',
                  borderRadius: '4px',
                  color: '#fff',
                  background: '#ff575a',
                  border: 'none',
                  margin: '10px 10px 0 0',
                  display: 'inline-block',
                },
                on: {
                  click: this.handleShi.bind(this, monitorItem),
                },
              },
              '雙向視頻'
            ),
            h(
              'button',
              {
                style: {
                  padding: '5px 20px',
                  fontSize: '14px',
                  borderRadius: '4px',
                  color: '#fff',
                  background: '#ff575a',
                  border: 'none',
                  margin: '10px 0 0 0',
                  display: 'inline-block',
                },
                on: {
                  click: this.handleQuXiao.bind(this, monitorItem),
                },
              },
              '取消'
            ),
          ]),
          duration: 0,
          showClose: false,
        })
 
        //將通知實例放入       
 this.notifications[monitorItem.orderKey] = notify
        this.handleAudio()
      },
 //關(guān)閉當(dāng)前故障彈框
      handleQuXiao(monitorItem) {
        this.openDialogflase = false
        this.notifications[monitorItem.orderKey].close()
        delete this.notifications[monitorItem.orderKey]
      },
      //關(guān)閉所有彈窗
      closeAll() {
        let vue = this
        for (let key in vue.notifications) {
          vue.notifications[key].close()
          delete vue.notifications[key]
        }
      },

總結(jié)

到此這篇關(guān)于vue通知提醒消息的文章就介紹到這了,更多相關(guān)vue通知提醒消息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • 解決vue打包之后靜態(tài)資源圖片失效的問題

    解決vue打包之后靜態(tài)資源圖片失效的問題

    下面小編就為大家分享一篇解決vue打包之后靜態(tài)資源圖片失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • VUE中如何渲染Echarts動畫柱狀圖

    VUE中如何渲染Echarts動畫柱狀圖

    這篇文章主要介紹了VUE中如何渲染Echarts動畫柱狀圖問題,具有很好的參考價值,希望對大家有所幫助。
    2023-03-03
  • Vue前端高效開發(fā)之列表渲染指令

    Vue前端高效開發(fā)之列表渲染指令

    這篇文章主要給大家介紹了關(guān)于Vue前端高效開發(fā)之列表渲染指令的相關(guān)資料,vue.js 使用的是 v-for 指令來處理組件元素的循環(huán)迭代邏輯,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-06-06
  • vue實現(xiàn)把接口單獨存放在一個文件方式

    vue實現(xiàn)把接口單獨存放在一個文件方式

    這篇文章主要介紹了vue實現(xiàn)把接口單獨存放在一個文件方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解axios全局路由攔截的設(shè)置方法

    詳解axios全局路由攔截的設(shè)置方法

    這篇文章主要介紹了axios全局路由攔截的設(shè)置方法,axios全局路由代碼通常是在構(gòu)建axios實例注入的,本文通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Vue3 composition API實現(xiàn)邏輯復(fù)用的方法

    Vue3 composition API實現(xiàn)邏輯復(fù)用的方法

    本文主要介紹了Vue3 composition API實現(xiàn)邏輯復(fù)用的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue實現(xiàn)與安卓、IOS交互的方法

    vue實現(xiàn)與安卓、IOS交互的方法

    這篇文章主要介紹了vue實現(xiàn)與安卓、IOS交互的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • vue頁面離開后執(zhí)行函數(shù)的實例

    vue頁面離開后執(zhí)行函數(shù)的實例

    下面小編就為大家分享一篇vue頁面離開后執(zhí)行函數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue+egg+jwt實現(xiàn)登錄驗證的示例代碼

    vue+egg+jwt實現(xiàn)登錄驗證的示例代碼

    這篇文章主要介紹了vue+egg+jwt實現(xiàn)登錄驗證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue中mixins混入的介紹與使用詳解

    Vue中mixins混入的介紹與使用詳解

    如果我們在每個組件中去重復(fù)定義這些屬性和方法會使得項目出現(xiàn)代碼冗余并提高了維護難度,針對這種情況官方提供了Mixins特性,這時使用Vue mixins混入有很大好處,下面就介紹下Vue mixins混入使用方法,需要的朋友參考下吧
    2022-12-12

最新評論