tryOnBeforeUnmount
tryOnBeforeUnmount 是一个实用函数,它安全地调用 onBeforeUnmount() 钩子。如果代码在 Vue 组件的生命周期内执行,它会像 onBeforeUnmount() 一样工作;否则,它什么也不会做。
用法
import { tryOnBeforeUnmount } from '@vueuse/core'
tryOnBeforeUnmount(() => {
// 这段代码将在组件卸载前执行,如果不在组件生命周期内,则不会执行。
console.log('组件即将卸载')
})
类型声明
/**
* Call onBeforeUnmount() if it's inside a component lifecycle, if not, do nothing
*
* @param fn
* @param target
*/
export declare function tryOnBeforeUnmount(fn: Fn, target?: any): void