Skip to content

useImage

Category
Export Size
1.19 kB
Last Changed
10 months ago

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 lang="ts">
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
ts
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"]
/** Image width */
width
?: HTMLImageElement["width"]
/** Image height */
height
?: HTMLImageElement["height"]
/** https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#decoding */
decoding
?: HTMLImageElement["decoding"]
/** Provides a hint of the relative priority to use when fetching the image */
fetchPriority
?: HTMLImageElement["fetchPriority"]
/** Provides a hint of the importance of the image */
ismap
?: HTMLImageElement["isMap"]
/** The partial URL (starting with #) of an image map associated with the element */
usemap
?: HTMLImageElement["useMap"]
} /** * 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, any[], true>
export type
UseImageReturn
=
ReturnType
<typeof
useImage
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
btea
Jelf
IlyaL
SerKo
IlyaL
Riley Gelwicks
François M
vaakian X
Lúcio Rubens

Changelog

v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.2.0-beta.1 on
4ca72 - feat: support more native option (#4173)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.2.0 on
04d32 - feat: support referrerPolicy option (#3132)

Released under the MIT License.

END OF YEAR SALE
Get 50% OFF + 3 Premium Bonuses
2 AI Courses + Vue Certification
Claim Now
TIME LIMITED OFFER