tryOnBeforeMount
tryOnBeforeMount 是一个实用的函数,它安全地调用 onBeforeMount() 钩子。如果代码在 Vue 组件的生命周期内执行,它会像 onBeforeMount() 一样工作;否则,它会立即执行传入的函数。
用法
import { tryOnBeforeMount } from '@vueuse/core'
tryOnBeforeMount(() => {
// 这段代码将在组件挂载前执行,如果不在组件生命周期内,则立即执行。
console.log('组件即将挂载或代码立即执行')
})
类型声明
/**
* Call onBeforeMount() if it's inside a component lifecycle, if not, just call the function
*
* @param fn
* @param sync if set to false, it will run in the nextTick() of Vue
* @param target
*/
export declare function tryOnBeforeMount(
fn: Fn,
sync?: boolean,
target?: any,
): void