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