Category | Utilities |
---|---|
Export Size | 212 B |
Last Changed | 2 months ago |
Utility for creating event hooks
Creating a function that uses createEventHook
import { createEventHook } from '@vueuse/core' export function useMyFetch(url) { const fetchResult = createEventHook<Response>() const fetchError = createEventHook<any>() fetch(url) .then(result => fetchResult.trigger(result)) .catch(error => fetchError.trigger(error.message)) return { onResult: fetchResult.on, onError: fetchError.on, } }
Using a function that uses createEventHook
<script setup lang="ts"> import { useMyFetch } from './my-fetch-function' const { onResult, onError } = useMyFetch('my api url') onResult((result) => { console.log(result) }) onError((error) => { console.error(error) }) </script>
export type EventHookOn<T = any> = (fn: (param: T) => void) => { off: () => void } export type EventHookOff<T = any> = (fn: (param: T) => void) => void export type EventHookTrigger<T = any> = (param: T) => Promise<unknown[]> export interface EventHook<T = any> { on: EventHookOn<T> off: EventHookOff<T> trigger: EventHookTrigger<T> } /** * Utility for creating event hooks * * @see https://vueuse.org/createEventHook */ export declare function createEventHook<T = any>(): EventHook<T>
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/shared/createEventHook/