mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
feat(packages/vue): add build config
This commit is contained in:
@@ -6,4 +6,3 @@ export * from './useMounted';
|
||||
export * from './useRenderCount';
|
||||
export * from './useSupported';
|
||||
export * from './useSyncRefs';
|
||||
export * from './useToggle';
|
||||
|
||||
38
packages/vue/src/composables/tryOnMounted/index.ts
Normal file
38
packages/vue/src/composables/tryOnMounted/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { onMounted, nextTick, type ComponentInternalInstance } from 'vue';
|
||||
import { getLifeCycleTarger } from '../../utils';
|
||||
|
||||
export interface TryOnMountedOptions {
|
||||
sync?: boolean;
|
||||
target?: ComponentInternalInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name tryOnMounted
|
||||
* @category Components
|
||||
* @description Calls a function if it's inside a component lifecycle hook, otherwise just calls it
|
||||
*
|
||||
* @param {Function} fn The function to call
|
||||
* @param {TryOnMountedOptions} [options={}] The options for the try on mounted function
|
||||
* @returns {void}
|
||||
*
|
||||
* @example
|
||||
* tryOnMounted(() => console.log('Mounted!'));
|
||||
*
|
||||
* @example
|
||||
* tryOnMounted(() => console.log('Mounted!'), { sync: false });
|
||||
*/
|
||||
export function tryOnMounted(fn: () => void, options: TryOnMountedOptions = {}) {
|
||||
const instance = getLifeCycleTarger();
|
||||
|
||||
const {
|
||||
sync = true,
|
||||
target,
|
||||
} = options;
|
||||
|
||||
if (instance)
|
||||
onMounted(fn, target);
|
||||
else if (sync)
|
||||
fn();
|
||||
else
|
||||
nextTick(fn);
|
||||
}
|
||||
Reference in New Issue
Block a user