解决关闭服务监控页面后还会继续请求数据

This commit is contained in:
不做码农 2022-05-29 14:18:50 +08:00
parent 27870f5f62
commit 8e692a97b8

View File

@ -1,22 +1,21 @@
<template> <template>
<div class="app-container"> <div class="app-container">
<el-row> <el-row>
<el-col :lg="24" class="card-box"> <el-col :lg="24" class="card-box" v-if="server.cpu">
<el-card class="box-card"> <el-card class="box-card">
<template #header> <template #header>
<span>状态</span> <span>状态</span>
</template> </template>
<el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6"> <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8" class="mr20">
<div class="title">CPU使用率</div> <div class="title">CPU使用率</div>
<el-tooltip placement="top-end"> <div class="content">
<div class="content" v-if="server.cpu">
<el-progress type="dashboard" :percentage="parseFloat(server.cpu.cpuRate)" /> <el-progress type="dashboard" :percentage="parseFloat(server.cpu.cpuRate)" />
</div> </div>
</el-tooltip>
<div class="footer" v-if="server.sys">{{ server.sys.cpuNum }} 核心</div> <div class="footer" v-if="server.sys">{{ server.sys.cpuNum }} 核心</div>
</el-col> </el-col>
<el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6" v-if="server.cpu">
<el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
<div class="title">内存使用率</div> <div class="title">内存使用率</div>
<el-tooltip placement="top-end"> <el-tooltip placement="top-end">
<template #content> <template #content>
@ -193,66 +192,50 @@
</div> </div>
</template> </template>
<script> <script setup name="server">
import { getServer } from '@/api/monitor/server' import { getServer } from '@/api/monitor/server'
import { onBeforeRouteLeave } from 'vue-router'
export default { onBeforeRouteLeave((to) => {
name: 'Server', clear()
data() { })
return { //
// const server = ref([])
loading: [], const intervalId = ref(null)
// const { proxy } = getCurrentInstance()
server: [], /** 查询服务器信息 */
intervalId: null, function getList() {
}
},
created() {
this.getList()
this.openLoading()
this.dataRefreh()
},
methods: {
/** 查询服务器信息 */
getList() {
getServer() getServer()
.then((response) => { .then((response) => {
this.server = response.data server.value = response.data
this.loading.close() proxy.$modal.closeLoading()
}) })
.catch((err) => { .catch((err) => {
this.loading.close() proxy.$modal.closeLoading()
}) })
}, }
// //
openLoading() { function openLoading() {
this.loading = this.$loading({ proxy.$modal.loading('拼命读取中')
lock: true, }
text: '拼命读取中', function dataRefreh() {
spinner: 'el-icon-loading', if (intervalId.value != null) {
background: 'rgba(0, 0, 0, 0.7)',
})
},
dataRefreh() {
if (this.intervalId != null) {
return return
} }
this.intervalId = setInterval(() => { intervalId.value = setInterval(() => {
this.getList() getList()
}, 10000) }, 10000)
}, }
/** /**
* 停止定时器 * 停止定时器
*/ */
clear() { function clear() {
clearInterval(this.intervalId) clearInterval(intervalId.value)
this.intervalId = null intervalId.value = null
},
},
destroyed() {
this.clear()
},
} }
getList()
openLoading()
dataRefreh()
</script> </script>
<style scoped> <style scoped>
table tr { table tr {