コンテンツへスキップ

useImage

カテゴリ
エクスポートサイズ
1.39 kB
最終更新
先週

ブラウザで画像をリアクティブに読み込みます。結果が表示されるのを待つか、フォールバックを表示できます。

デモ

読み込み中...

使用法

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"]
}
/**
 * Reactive load an image in the browser, you can wait the result to display it or show a fallback.
 *
 * @see https://vueuse.dokyumento.jp/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>

ソース

ソースデモドキュメント

貢献者

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

変更履歴

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2 のサポートを削除、バンドルを最適化、クリーンアップ (#4349)
v10.2.0 2023/06/16
04d32 - feat: referrerPolicy オプションのサポート (#3132)
v10.1.1 2023/05/01
c110a - feat: より多くの画像プロパティのサポート (#3021)
v10.0.0-beta.4 2023/04/13
4d757 - feat(types)!: MaybeComputedRefMaybeRefOrGetter にリネーム
0a72b - feat(toValue): resolveUnreftoValue にリネーム
v9.11.0 2023/01/17
d5321 - fix(components): defineComponent を pure としてマーク (#2623)

MITライセンスの下で公開されています。