Skip to content

Commit

Permalink
fix(DarkToggle): 组件生命结束取消监听
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhankui committed Feb 11, 2023
1 parent 461b8cf commit 8f0f3e8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions frontend/components/Uno/DarkToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@ if (process.client) {
setItem()
}
onMounted(() => {
// 上面操作只会在页面加载时才会生效,因此,需要给media添加事件监听器
media.addEventListener('change', (e) => {
if (e.matches)
setDark()
else
setLight()
})
media.addEventListener('change', (e) => {
if (e.matches)
setDark()
else
setLight()
})
window.addEventListener('storage', (e) => {
if (e.newValue === 'light')
setLight()
window.addEventListener('storage', (e) => {
if (e.newValue === 'light')
setLight()
else
setDark()
else
setDark()
})
})
onUnmounted(() => {
media.removeEventListener('change', (e) => {}, true)
window.removeEventListener('storage', (e) => {}, true)
})
}
</script>
Expand Down

0 comments on commit 8f0f3e8

Please sign in to comment.