Skip to content

useImage ​

Category
Export Size
1.39 kB
Last Changed
4 months ago

Reactive load an image in the browser, you can wait the result to display it or show a fallback.

Demo ​

Loading...

Usage ​

html
<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>
<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.

html
<template>
  <UseImage src="https://place.dog/300/200">
    <template #loading>
      Loading..
    </template>

    <template #error>
      Failed
    </template>
  </UseImage>
</template>
<template>
  <UseImage src="https://place.dog/300/200">
    <template #loading>
      Loading..
    </template>

    <template #error>
      Failed
    </template>
  </UseImage>
</template>

Source ​

Source • Demo • Docs

Contributors ​

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

Changelog ​

v10.2.0 on 6/16/2023
04d32 - feat: support referrerPolicy option (#3132)
v10.1.1 on 5/1/2023
c110a - feat: support more image properties (#3021)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v8.9.2 on 7/12/2022
24ede - fix(useAsyncState)!: rename type AsyncStateOptions to UseAsyncStateOptions (#1809)
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.7.0 on 6/16/2022
f4b37 - feat: new function (#1460)

Released under the MIT License.