vue-ahooks
- 安装: npm install vue-ahooks
import {
useAwaitTo,
useDebounceFn,
useThrottleFn,
useMap,
useLockFn,
useToggle,
useTimeout,
useRafTimeout,
useDebounceWatch,
} from "vue-ahooks";
/**
* @ const [state, { set, toggle }] = useToggle(defaultValue, reverseValue)
* @ state 当前状态
* @ toggle 根据defaultValue,reverseValue反转
*/
const [state, { set }] = useToggle();
/**
* @ params requset(Promise)
* @ return err(异常 | null), data(Promise成功状态下数据)
*/
const [err, data] = await useAwaitTo(requset());
/**
* @ params fn(需装饰函数)
* @ return run(执行函数)
*/
const run = await useLockFn(fn);
/**
* @ params fn(需装饰函数), time(节流间隔)
* @ return run(已被装饰函数)
*/
const [run] = useThrottleFn(fn, time);
/**
* @ params fn(需装饰函数), time(防抖间隔)
* @ return run(已被装饰函数)
*/
const [run] = useDebounceFn(fn, time);
/**
* @ params initialValue?: 初始函数
* @ return type Actions<K, T> = {
set: (key: K, value: T) => void
get: (key: K) => T | undefined
remove: (key: K) => void
has: (key: K) => boolean
clear: () => void
setAll: (newMap: MapValue<K, T>) => void
reset: () => void
}
*/
const [Map, Actions] = useMap(initialValue);
/**
* @ params deps(依赖) effect(执行函数) options(同watch参数)
* @ return Null
}
*/
useDebounceWatch(deps, effect, options);
/**
* @ const clearTimeout = useRafTimeout(fn, delay)
* @ fn(待执行函数), delay(等待秒数,默认16)
* @ return clearTimeout
*/
useRafTimeout(() => {
fn();
}, 1000);