Clear timing events and event listeners when component unmounts

This commit is contained in:
Aroooba 2024-01-09 14:04:55 +09:00 committed by chuzhixin
parent 1e8dc5bb43
commit b139759c38
11 changed files with 74 additions and 20 deletions

View File

@ -76,12 +76,16 @@
async refreshRoute() { async refreshRoute() {
this.$baseEventBus.$emit('reload-router-view') this.$baseEventBus.$emit('reload-router-view')
this.pulse = true this.pulse = true
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.pulse = false this.pulse = false
}, 1000) }, 1000)
}, },
}, },
}
beforeDestroy() {
clearTimeout(this.timeOutID);
}
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.top-bar-container { .top-bar-container {

View File

@ -58,13 +58,19 @@
}, },
}, },
created() { created() {
// const handleReloadRouterView = () => {
this.$baseEventBus.$on('reload-router-view', () => {
this.routerView = false this.routerView = false
this.$nextTick(() => { this.$nextTick(() => {
this.routerView = true this.routerView = true
}) })
}) };
//
this.$baseEventBus.$on('reload-router-view', handleReloadRouterView)
this.$once('hook:beforeDestroy', () => {
this.$baseEventBus.$off('reload-router-view', handleReloadRouterView);
});
}, },
mounted() {}, mounted() {},
methods: { methods: {

View File

@ -38,6 +38,7 @@
data() { data() {
return { return {
pulse: false, pulse: false,
timeOutID: null
} }
}, },
computed: { computed: {
@ -58,12 +59,16 @@
async refreshRoute() { async refreshRoute() {
this.$baseEventBus.$emit('reload-router-view') this.$baseEventBus.$emit('reload-router-view')
this.pulse = true this.pulse = true
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.pulse = false this.pulse = false
}, 1000) }, 1000)
}, },
}, },
}
beforeDestroy() {
clearTimeout(this.timeOutID);
}
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -79,9 +79,11 @@
}), }),
}, },
created() { created() {
this.$baseEventBus.$on('theme', () => { const handleTheme = () => {
this.handleOpenThemeBar() this.handleOpenThemeBar()
}) };
this.$baseEventBus.$on('theme', handleTheme)
const theme = localStorage.getItem('vue-admin-beautiful-theme') const theme = localStorage.getItem('vue-admin-beautiful-theme')
if (null !== theme) { if (null !== theme) {
this.theme = JSON.parse(theme) this.theme = JSON.parse(theme)
@ -91,6 +93,10 @@
this.theme.header = this.header this.theme.header = this.header
this.theme.tabsBar = this.tabsBar this.theme.tabsBar = this.tabsBar
} }
this.$once('hook:beforeDestroy', () => {
this.$baseEventBus.$off('theme', handleTheme);
});
}, },
methods: { methods: {
...mapActions({ ...mapActions({

View File

@ -51,7 +51,11 @@
export default { export default {
name: 'Layout', name: 'Layout',
data() { data() {
return { oldLayout: '' } return {
oldLayout: '',
controller: new window.AbortController(),
timeOutID: null
};
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
@ -72,6 +76,8 @@
}, },
beforeDestroy() { beforeDestroy() {
window.removeEventListener('resize', this.handleResize) window.removeEventListener('resize', this.handleResize)
this.controller.abort();
clearTimeout(this.timeOutID);
}, },
mounted() { mounted() {
this.oldLayout = this.layout this.oldLayout = this.layout
@ -90,7 +96,7 @@
this.$store.dispatch('settings/changeLayout', this.oldLayout) this.$store.dispatch('settings/changeLayout', this.oldLayout)
} }
this.$store.dispatch('settings/toggleDevice', 'mobile') this.$store.dispatch('settings/toggleDevice', 'mobile')
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.$store.dispatch('settings/foldSideBar') this.$store.dispatch('settings/foldSideBar')
}, 2000) }, 2000)
} else { } else {
@ -103,7 +109,10 @@
if (e.key === tokenName || e.key === null) window.location.reload() if (e.key === tokenName || e.key === null) window.location.reload()
if (e.key === tokenName && e.value === null) window.location.reload() if (e.key === tokenName && e.value === null) window.location.reload()
}, },
false {
capture: false,
signal: this.controller?.signal
}
) )
}) })
}, },

View File

@ -103,6 +103,7 @@
loading: false, loading: false,
passwordType: 'password', passwordType: 'password',
redirect: undefined, redirect: undefined,
timeOutID: null,
} }
}, },
watch: { watch: {
@ -118,11 +119,12 @@
}, },
beforeDestroy() { beforeDestroy() {
document.body.style.overflow = 'auto' document.body.style.overflow = 'auto'
clearTimeout(this.timeOutID)
}, },
mounted() { mounted() {
this.form.username = 'admin' this.form.username = 'admin'
this.form.password = '123456' this.form.password = '123456'
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.handleLogin() this.handleLogin()
}, 3000) }, 3000)
}, },

View File

@ -101,6 +101,7 @@
list: [], list: [],
listLoading: true, listLoading: true,
elementLoadingText: '正在加载...', elementLoadingText: '正在加载...',
timeOutID: null,
} }
}, },
async created() { async created() {
@ -108,6 +109,10 @@
this.data = roleData.data this.data = roleData.data
this.fetchData() this.fetchData()
}, },
beforeDestroy() {
clearTimeout(this.timeOutID)
},
methods: { methods: {
handleEdit(row) { handleEdit(row) {
if (row.path) { if (row.path) {
@ -130,7 +135,7 @@
const { data } = await getList() const { data } = await getList()
this.list = data this.list = data
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.listLoading = false this.listLoading = false
}, 300) }, 300)
}, },

View File

@ -64,11 +64,16 @@
pageSize: 10, pageSize: 10,
permission: '', permission: '',
}, },
timeOutID: null,
} }
}, },
created() { created() {
this.fetchData() this.fetchData()
}, },
beforeDestroy() {
clearTimeout(this.timeOutID)
},
methods: { methods: {
setSelectRows(val) { setSelectRows(val) {
this.selectRows = val this.selectRows = val
@ -118,7 +123,7 @@
const { data, totalCount } = await getList(this.queryForm) const { data, totalCount } = await getList(this.queryForm)
this.list = data this.list = data
this.total = totalCount this.total = totalCount
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.listLoading = false this.listLoading = false
}, 300) }, 300)
}, },

View File

@ -72,11 +72,16 @@
pageSize: 10, pageSize: 10,
username: '', username: '',
}, },
timeOutID: null,
} }
}, },
created() { created() {
this.fetchData() this.fetchData()
}, },
beforeDestroy() {
clearTimeout(this.timeOutID)
},
methods: { methods: {
setSelectRows(val) { setSelectRows(val) {
this.selectRows = val this.selectRows = val
@ -126,7 +131,7 @@
const { data, totalCount } = await getList(this.queryForm) const { data, totalCount } = await getList(this.queryForm)
this.list = data this.list = data
this.total = totalCount this.total = totalCount
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.listLoading = false this.listLoading = false
}, 300) }, 300)
}, },

View File

@ -53,6 +53,8 @@
startVal: 0, startVal: 0,
endVal: 20, endVal: 20,
timeInterval: null, timeInterval: null,
timeOutID: null,
timeOutID2: null,
} }
}, },
mounted() { mounted() {
@ -69,17 +71,19 @@
if (this.clearInterval) { if (this.clearInterval) {
clearInterval(this.timeInterval) clearInterval(this.timeInterval)
} }
clearTimeout(this.timeOutID)
clearTimeout(this.timeOutID2)
}, },
methods: { methods: {
handleProfile() { handleProfile() {
this.profileShow = false this.profileShow = false
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.profileShow = true this.profileShow = true
}) })
}, },
handleSolidText() { handleSolidText() {
this.solidTextShow = false this.solidTextShow = false
setTimeout(() => { this.timeOutID2 = setTimeout(() => {
this.solidTextShow = true this.solidTextShow = true
}) })
}, },

View File

@ -109,6 +109,7 @@
pageSize: 20, pageSize: 20,
title: '', title: '',
}, },
timeOutID: null,
} }
}, },
computed: { computed: {
@ -119,7 +120,9 @@
created() { created() {
this.fetchData() this.fetchData()
}, },
beforeDestroy() {}, beforeDestroy() {
clearTimeout(this.timeOutID)
},
mounted() {}, mounted() {},
methods: { methods: {
tableSortChange() { tableSortChange() {
@ -181,7 +184,7 @@
}) })
this.imageList = imageList this.imageList = imageList
this.total = totalCount this.total = totalCount
setTimeout(() => { this.timeOutID = setTimeout(() => {
this.listLoading = false this.listLoading = false
}, 500) }, 500)
}, },