useImage
ブラウザで画像をリアクティブに読み込みます。結果が表示されるのを待つか、フォールバックを表示できます。
デモ
読み込み中...
使用法
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>
ソース
貢献者
変更履歴
v12.0.0-beta.1
2024/11/21v10.2.0
2023/06/16v10.1.1
2023/05/01v10.0.0-beta.4
2023/04/134d757
- feat(types)!: MaybeComputedRef
を MaybeRefOrGetter
にリネーム0a72b
- feat(toValue): resolveUnref
を toValue
にリネームv9.11.0
2023/01/17