diff --git a/src/views/system/LoggingView.vue b/src/views/system/LoggingView.vue index c8e649d5..d5b72c97 100644 --- a/src/views/system/LoggingView.vue +++ b/src/views/system/LoggingView.vue @@ -4,11 +4,14 @@ import store from '@/store' // 日志列表 const logs = ref([]) +// SSE消息对象 +let eventSource: EventSource | null = null + // SSE持续获取日志 function startSSELogging() { const token = store.state.auth.token if (token) { - const eventSource = new EventSource( + eventSource = new EventSource( `${import.meta.env.VITE_API_BASE_URL}system/logging?token=${token}`, ) @@ -17,10 +20,6 @@ function startSSELogging() { if (message) logs.value.push(message) }) - - onBeforeUnmount(() => { - eventSource.close() - }) } } @@ -65,6 +64,11 @@ const extractLogDetails = computed(() => { onMounted(() => { startSSELogging() }) + +onBeforeUnmount(() => { + if (eventSource) + eventSource.close() +})