跳到主要内容

useImage

分类
导出大小
1.19 kB
上次更改
8 个月前

在浏览器中响应式加载图片,你可以等待结果显示图片或显示一个备用图片。

演示

加载中...

用法

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>

组件用法

此函数还通过 @vueuse/components 包提供了一个无渲染组件版本。了解更多用法

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

类型声明

显示类型声明
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://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Referrer-Policy */
referrerPolicy
?: HTMLImageElement["referrerPolicy"]
/** Image width */
width
?: HTMLImageElement["width"]
/** Image height */
height
?: HTMLImageElement["height"]
/** https://mdn.org.cn/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.cn/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
SerKo
IlyaL
Riley Gelwicks
François M
vaakian X
Lúcio Rubens

更新日志

8c521 - feat(components)!: 重构组件并使其保持一致 (#4912)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
4ca72 - feat: 支持更多原生选项 (#4173)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.2.0
04d32 - feat: 支持 referrerPolicy 选项 (#3132)
v10.1.1
c110a - feat: 支持更多图片属性 (#3021)

根据 MIT 许可证发布。