使用vuetify实现全局v-alert消息通知的方法
作者:DingDangDog
使用强大的Vuetify开发前端页面,结果发现官方没有提供简便的全局消息通知组件,所以自己动手写了一个简单的组件,接下来通过本文给大家介绍使用vuetify实现全局v-alert消息通知的详细代码,感兴趣的朋友跟随小编一起看看吧
简介
使用强大的Vuetify开发前端页面,结果发现官方没有提供简便的全局消息通知组件(像Element中的ElMessage那样),所以自己动手写了一个简单的组件,效果如下:

PS:如果是我没找到官方版本,请评论告诉我!下面直接上代码
组件封装
全局变量:alert.ts
该文件可视为util文件,但我将其放在了stores文件夹下,主要提供了两个作用:
- 定义
newAlert全局变量,用于临时存储新Alert信息。 - 定义常用的全局方法,用于快速创建
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二次封装的组件,主要提供了以下几个功能:
- 监听
newAlert值的变化,当值变化时,根据当时newAlert更新alertMap; - 遍历
alertMap创建多个v-alert; - 定时删除历史
v-alert; - css定义
v-alert的显示位置。 
<script setup lang="ts">
import { ref, watch } from 'vue'
import { type AlertInfo, newAlert } from '@/stores/alert'
// 定义 Map,存储Alert信息集合,使用Map便于删除
const alertMap = ref<Map<string, AlertInfo>>(new Map)
// 监听新Alert创建
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>调用测试
有了以上两个文件后,即可调用测试
引用组件
- 要想全局显示通知,则需要在项目顶层文件中引用该组件,我是在
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>调用工具方法
- 组件引用后,即可在项目任意地方调用工具方法测试效果,调用方式如下:
 
<script setup lang="ts">
// 引入封装好的工具方法
import { successAlert, errorAlert, infoAlert, warningAlert } from '@/stores/alert'
</script>
<template>
  <!-- 按钮直接调用测试 -->
  <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>可能存在的问题
- 可能存在并发问题:没有进行并发测试;
 - 可能存在响应式问题:本示例仅在桌面端测试过,可能无法适配手机、平板等终端;
 
到此这篇关于使用vuetify实现全局v-alert消息通知的方法的文章就介绍到这了,更多相关vuetify 全局v-alert消息通知内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
