WebIndex/components/alert/Notification.vue

49 lines
1.2 KiB
Vue
Raw Normal View History

2024-12-01 13:09:44 +08:00
<script lang="ts" setup>
2025-01-13 21:51:37 +08:00
const { $mitt } = useNuxtApp(),
notifications = ref<INotification[]>([]),
typeClasses = {
success: 'bg-green-100 text-green-800 border border-green-400',
error: 'bg-red-100 text-red-800 border border-red-400',
warning: 'bg-yellow-100 text-yellow-800 border border-yellow-400',
info: 'bg-blue-100 text-blue-800 border border-blue-400',
},
addNotification = (notification: INotification) => {
notifications.value.push(notification)
// 自动移除通知
setTimeout(() => {
notifications.value.shift()
}, 5000)
console.log(1)
}
2024-12-01 13:09:44 +08:00
2024-12-18 08:42:00 +08:00
$mitt.on('eventBus', addNotification)
2024-12-01 13:09:44 +08:00
onBeforeUnmount(() => {
2025-01-13 21:51:37 +08:00
$mitt.off('eventBus', addNotification)
})
2024-12-01 13:09:44 +08:00
</script>
<template>
2024-12-18 08:44:37 +08:00
<div class="fixed top-5 right-5 z-50 space-y-4">
2024-12-01 13:09:44 +08:00
<div
2025-01-13 21:51:37 +08:00
v-for="(notification, index) in notifications"
:key="index"
:class="[
2024-12-01 13:09:44 +08:00
'p-4 rounded-lg shadow-lg transition-all duration-300',
2025-01-13 21:51:37 +08:00
typeClasses[notification.level],
2024-12-01 13:09:44 +08:00
]"
>
2025-01-13 21:51:37 +08:00
<h3 class="text-lg font-bold">
{{ notification.title }}
</h3>
2024-12-01 13:09:44 +08:00
<p>{{ notification.message }}</p>
</div>
</div>
</template>
<style scoped>
</style>