使用vuetify實(shí)現(xiàn)全局v-alert消息通知的方法
簡(jiǎn)介
使用強(qiáng)大的Vuetify開(kāi)發(fā)前端頁(yè)面,結(jié)果發(fā)現(xiàn)官方?jīng)]有提供簡(jiǎn)便的全局消息通知組件(像Element中的ElMessage那樣),所以自己動(dòng)手寫了一個(gè)簡(jiǎn)單的組件,效果如下:

PS:如果是我沒(méi)找到官方版本,請(qǐng)?jiān)u論告訴我!下面直接上代碼
組件封裝
全局變量:alert.ts
該文件可視為util文件,但我將其放在了stores文件夾下,主要提供了兩個(gè)作用:
- 定義
newAlert全局變量,用于臨時(shí)存儲(chǔ)新Alert信息。 - 定義常用的全局方法,用于快速創(chuàng)建
alert,如:successAlert、errorAlert等。
import { ref } from 'vue'
export interface AlertInfo {
id: string,
type: string,
message: string
}
export const newAlert = ref<AlertInfo>({
id: 'alert' + 0,
type: '',
message: ''
})
export const alert = (type: string, message: string) => {
newAlert.value.id = Math.random().toString()
newAlert.value.type = type
newAlert.value.message = message
}
export const errorAlert = (message: string) => {
alert('error', message)
}
export const successAlert = (message: string) => {
alert('success', message)
}
export const infoAlert = (message: string) => {
alert('info', message)
}
export const warningAlert = (message: string) => {
alert('warning', message)
}組件:GlobalAlert.vue
該文件是v-alert二次封裝的組件,主要提供了以下幾個(gè)功能:
- 監(jiān)聽(tīng)
newAlert值的變化,當(dāng)值變化時(shí),根據(jù)當(dāng)時(shí)newAlert更新alertMap; - 遍歷
alertMap創(chuàng)建多個(gè)v-alert; - 定時(shí)刪除歷史
v-alert; - css定義
v-alert的顯示位置。
<script setup lang="ts">
import { ref, watch } from 'vue'
import { type AlertInfo, newAlert } from '@/stores/alert'
// 定義 Map,存儲(chǔ)Alert信息集合,使用Map便于刪除
const alertMap = ref<Map<string, AlertInfo>>(new Map)
// 監(jiān)聽(tīng)新Alert創(chuàng)建
watch(newAlert.value, () => {
alertMap.value.set(newAlert.value.id, { ...newAlert.value })
console.log(alertMap.value)
deleteAlert(newAlert.value.id)
})
const deleteAlert = (id: string) => {
console.log(id)
setTimeout(() => {
alertMap.value.delete(id)
}, 3000)
}
</script>
<template>
<div class="alert-container">
<v-alert
class="v-alert"
v-for="(alert, index) in Array.from(alertMap.values())"
:key="index"
:type="alert.type"
border="start"
variant="tonal"
closable
close-label="Close Alert"
:text="alert.message"
>
</v-alert>
</div>
</template>
<style scoped>
.alert-container {
position: absolute;
top: 8%;
right: 5%;
}
.v-alert {
margin-top: 0.2rem !important;
}
</style>調(diào)用測(cè)試
有了以上兩個(gè)文件后,即可調(diào)用測(cè)試
引用組件
- 要想全局顯示通知,則需要在項(xiàng)目頂層文件中引用該組件,我是在
App.vue中引用,如下:
<script setup lang="ts">
import { RouterView } from 'vue-router'
// 引用GlobalAlert
import GlobalAlert from '@/components/GlobalAlert.vue'
</script>
<template>
// 引用GlobalAlert
<GlobalAlert />
<RouterView />
</template>
<style scoped>
</style>調(diào)用工具方法
- 組件引用后,即可在項(xiàng)目任意地方調(diào)用工具方法測(cè)試效果,調(diào)用方式如下:
<script setup lang="ts">
// 引入封裝好的工具方法
import { successAlert, errorAlert, infoAlert, warningAlert } from '@/stores/alert'
</script>
<template>
<!-- 按鈕直接調(diào)用測(cè)試 -->
<v-btn variant="tonal"
@click="successAlert('success')">
success
</v-btn>
<v-btn variant="tonal"
@click="errorAlert('error')">
error
</v-btn>
<v-btn variant="tonal"
@click="infoAlert('info')">
info
</v-btn>
<v-btn variant="tonal"
@click="warningAlert('warning')">
warning
</v-btn>
</template>
<style scoped>
</style>可能存在的問(wèn)題
- 可能存在并發(fā)問(wèn)題:沒(méi)有進(jìn)行并發(fā)測(cè)試;
- 可能存在響應(yīng)式問(wèn)題:本示例僅在桌面端測(cè)試過(guò),可能無(wú)法適配手機(jī)、平板等終端;
到此這篇關(guān)于使用vuetify實(shí)現(xiàn)全局v-alert消息通知的方法的文章就介紹到這了,更多相關(guān)vuetify 全局v-alert消息通知內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)動(dòng)態(tài)路由的示例詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)知識(shí),主要涉及到兩個(gè)方面:一是路由的動(dòng)態(tài)添加,二是基于路由的參數(shù)變化來(lái)動(dòng)態(tài)渲染組件,下面就跟隨小編一起深入學(xué)習(xí)一下動(dòng)態(tài)路由的實(shí)現(xiàn)吧2024-02-02
詳解vue-cli項(xiàng)目在IE瀏覽器打開(kāi)報(bào)錯(cuò)解決方法
這篇文章主要介紹了詳解vue-cli項(xiàng)目在IE瀏覽器打開(kāi)報(bào)錯(cuò)解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
vue+elementUi實(shí)現(xiàn)點(diǎn)擊地圖自動(dòng)填充經(jīng)緯度以及地點(diǎn)
這篇文章主要為大家詳細(xì)介紹了vue+elementUi實(shí)現(xiàn)點(diǎn)擊地圖自動(dòng)填充經(jīng)緯度以及地點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
解決element-ui el-input賦值后不能編輯的問(wèn)題
這篇文章主要介紹了解決element-ui el-input賦值后不能編輯的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue組件數(shù)據(jù)傳遞與props校驗(yàn)方式
這篇文章主要介紹了Vue組件數(shù)據(jù)傳遞與props校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue 讀取HTMLCollection列表的length為0問(wèn)題
這篇文章主要介紹了Vue 讀取HTMLCollection列表的length為0問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法
這篇文章主要介紹了詳解Vue項(xiàng)目在其他電腦npm run dev運(yùn)行報(bào)錯(cuò)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
如何利用Echarts根據(jù)經(jīng)緯度給地圖畫點(diǎn)畫線
最近工作中遇到了一個(gè)需求,需要利用echarts在地圖上面標(biāo)記點(diǎn)位,所下面這篇文章主要給大家介紹了關(guān)于如何利用Echarts根據(jù)經(jīng)緯度給地圖畫點(diǎn)畫線的相關(guān)資料,需要的朋友可以參考下2022-05-05

