跳到主要內容

useImage

分類
匯出大小
1.11 kB
最近更改
5 天前

在瀏覽器中響應式地載入圖片,您可以等待結果顯示或顯示備用圖片。

範例

載入中...

用法

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>

元件用法

此函式也透過 @vueuse/components 套件提供無渲染元件版本。 深入了解用法

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

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

類型宣告

顯示類型宣告
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"]
  /** 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.dev.org.tw/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>

原始碼

原始碼範例文件

貢獻者

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

更新日誌

v12.8.0 於 3/5/2025
7432f - 特性(類型):棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生 (#4636)
v12.3.0 於 1/2/2025
59f75 - 特性(toValue):棄用 @vueuse/shared 中的 toValue,改用 Vue 原生
v12.2.0-beta.1 於 12/23/2024
4ca72 - 特性:支援更多原生選項 (#4173)
v12.0.0-beta.1 於 11/21/2024
0a9ed - 特性!:移除 Vue 2 支援,最佳化捆綁包並清理 (#4349)
v10.2.0 於 6/16/2023
04d32 - 特性:支援 referrerPolicy 選項 (#3132)
v10.1.1 於 5/1/2023
c110a - 特性:支援更多圖片屬性 (#3021)
v10.0.0-beta.4 於 4/13/2023
4d757 - 特性(類型)!:重新命名 MaybeComputedRefMaybeRefOrGetter
0a72b - 特性(toValue):重新命名 resolveUnreftoValue

以 MIT 授權條款發布。