useImage ​
Reactive load an image in the browser, you can wait the result to display it or show a fallback.
Demo ​
Loading...
Usage ​
vue
<script setup>
import { useImage } from '@vueuse/core'
const avatarUrl = 'https://place.dog/300/200'
const { isLoading } = useImage({ src: avatarUrl })
</script>
<template>
<span v-if="isLoading">Loading</span>
<img v-else :src="avatarUrl">
</template>
Component Usage ​
This function also provides a renderless component version via the
@vueuse/components
package. Learn more about the usage.
vue
<template>
<UseImage src="https://place.dog/300/200">
<template #loading>
Loading..
</template>
<template #error>
Failed
</template>
</UseImage>
</template>
Type Declarations ​
Show Type Declarations
typescript
export interface UseImageOptions {
/** Address of the resource */
src: string
/** Images to use in different situations, e.g., high-resolution displays, small monitors, etc. */
srcset?: string
/** Image sizes for different page layouts */
sizes?: string
/** Image alternative information */
alt?: string
/** Image classes */
class?: string
/** Image loading */
loading?: HTMLImageElement["loading"]
/** Image CORS settings */
crossorigin?: string
/** Referrer policy for fetch https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy */
referrerPolicy?: HTMLImageElement["referrerPolicy"]
}
/**
* Reactive load an image in the browser, you can wait the result to display it or show a fallback.
*
* @see https://vueuse.org/useImage
* @param options Image attributes, as used in the <img> tag
* @param asyncStateOptions
*/
export declare function useImage<Shallow extends true>(
options: MaybeRefOrGetter<UseImageOptions>,
asyncStateOptions?: UseAsyncStateOptions<Shallow>,
): UseAsyncStateReturn<HTMLImageElement | undefined, [], true>
export type UseImageReturn = ReturnType<typeof useImage>
Source ​
Contributors ​
Anthony Fu
Jelf
Anthony Fu
Riley Gelwicks
btea
François M
vaakian X
Lúcio Rubens
Changelog ​
v12.0.0-beta.1
on 11/21/2024v10.2.0
on 6/16/2023v10.1.1
on 5/1/2023v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: rename MaybeComputedRef
to MaybeRefOrGetter
0a72b
- feat(toValue): rename resolveUnref
to toValue
v9.11.0
on 1/17/2023