chore(backtop): 组件卸载时移除事件监听

This commit is contained in:
sight 2022-03-05 16:09:15 +08:00
parent a2b7db1418
commit d27179de02

View File

@ -5,7 +5,15 @@ export default {
</script> </script>
<script lang="ts" setup> <script lang="ts" setup>
import { ref, shallowRef, withDefaults, computed, onMounted } from "vue"; import {
ref,
shallowRef,
withDefaults,
computed,
onMounted,
onBeforeUnmount,
} from "vue";
import { useThrottle } from "@vueuse/core";
import LayIcon from "../icon/index"; import LayIcon from "../icon/index";
import "./index.less"; import "./index.less";
@ -186,6 +194,13 @@ onMounted(() => {
scrollTarget.value = getScrollTarget(); scrollTarget.value = getScrollTarget();
scrollTarget.value.addEventListener("scroll", throttle(handleScroll, 300)); scrollTarget.value.addEventListener("scroll", throttle(handleScroll, 300));
}); });
onBeforeUnmount(() => {
scrollTarget.value?.removeEventListener(
"scroll",
throttle(handleScroll, 300)
);
});
</script> </script>
<template> <template>