增強版的 img 標簽,提供多種圖片填充模式,支持圖片懶加載、加載中提示、加載失敗提示。
通過以下方式來全局注冊組件,更多注冊方式請參考組件注冊。
import { createApp } from 'vue';
import { Image as VanImage } from 'vant';
const app = createApp();
app.use(VanImage);
基礎用法與原生 ?img
? 標簽一致,可以設置 ?src
?、?width
?、?height
?、?alt
? 等原生屬性。
<van-image
width="100"
height="100"
src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
/>
通過 ?fit
? 屬性可以設置圖片填充模式,等同于原生的 object-fit 屬性,可選值見下方表格。
<van-image
width="10rem"
height="10rem"
fit="contain"
src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
/>
通過 ?position
? 屬性可以設置圖片位置,結合 ?fit
? 屬性使用,等同于原生的 object-position 屬性。
<van-image
width="10rem"
height="10rem"
fit="cover"
position="left"
src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
/>
通過 ?round
? 屬性可以設置圖片變圓,注意當圖片寬高不相等且 ?fit
? 為 ?contain
? 或 ?scale-down
? 時,將無法填充一個完整的圓形。
<van-image
round
width="10rem"
height="10rem"
src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
/>
設置 ?lazy-load
? 屬性來開啟圖片懶加載,需要搭配 Lazyload 組件使用。
<van-image
width="100"
height="100"
lazy-load
src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow"
/>
import { createApp } from 'vue';
import { Lazyload } from 'vant';
const app = createApp();
app.use(Lazyload);
?Image
? 組件提供了默認的加載中提示,支持通過 ?loading
? 插槽自定義內容。
<van-image src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<template v-slot:loading>
<van-loading type="spinner" size="20" />
</template>
</van-image>
?Image
? 組件提供了默認的加載失敗提示,支持通過 ?error
? 插槽自定義內容。
<van-image src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<template v-slot:error>加載失敗</template>
</van-image>
參數 | 說明 | 類型 | 默認值 |
---|---|---|---|
src | 圖片鏈接 | string | - |
fit | 圖片填充模式,等同于原生的 object-fit 屬性 | string | fill
|
position v3.4.2
|
圖片位置,等同于原生的 object-position 屬性,可選值為 top right bottom left 或 string
|
string | center
|
alt | 替代文本 | string | - |
width | 寬度,默認單位為 px
|
number | string | - |
height | 高度,默認單位為 px
|
number | string | - |
radius | 圓角大小,默認單位為 px
|
number | string | 0
|
round | 是否顯示為圓形 | boolean | false
|
block 3.6.3
|
是否將根節(jié)點設置為塊級元素,默認情況下為 inline-block 元素 |
boolean | false
|
lazy-load | 是否開啟圖片懶加載,須配合 Lazyload 組件使用 | boolean | false
|
show-error | 是否展示圖片加載失敗提示 | boolean | true
|
show-loading | 是否展示圖片加載中提示 | boolean | true
|
error-icon | 失敗時提示的圖標名稱或圖片鏈接,等同于 Icon 組件的 name 屬性 | string | photo-fail
|
loading-icon | 加載時提示的圖標名稱或圖片鏈接,等同于 Icon 組件的 name 屬性 | string | photo
|
icon-size v3.0.11
|
加載圖標和失敗圖標的大小 | number | string | 32px
|
icon-prefix | 圖標類名前綴,等同于 Icon 組件的 class-prefix 屬性 | string | van-icon
|
名稱 | 含義 |
---|---|
contain | 保持寬高縮放圖片,使圖片的長邊能完全顯示出來 |
cover | 保持寬高縮放圖片,使圖片的短邊能完全顯示出來,裁剪長邊 |
fill | 拉伸圖片,使圖片填滿元素 |
none | 保持圖片原有尺寸 |
scale-down | 取 none 或 contain 中較小的一個 |
事件名 | 說明 | 回調參數 |
---|---|---|
click | 點擊圖片時觸發(fā) | event: MouseEvent |
load | 圖片加載完畢時觸發(fā) | - |
error | 圖片加載失敗時觸發(fā) | - |
名稱 | 說明 |
---|---|
default | 自定義圖片下方的內容 |
loading | 自定義加載中的提示內容 |
error | 自定義加載失敗時的提示內容 |
組件導出以下類型定義:
import type { ImageFit, ImagePosition, ImageProps } from 'vant';
組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請參考 ConfigProvider 組件。
名稱 | 默認值 | 描述 |
---|---|---|
--van-image-placeholder-text-color | var(--van-text-color-2) | - |
--van-image-placeholder-font-size | var(--van-font-size-md) | - |
--van-image-placeholder-background | var(--van-background) | - |
--van-image-loading-icon-size | 32px | - |
--van-image-loading-icon-color | var(--van-gray-4) | - |
--van-image-error-icon-size | 32px | - |
--van-image-error-icon-color | var(--van-gray-4) | - |
在 .vue 文件中通過相對路徑引用本地圖片時,需要在圖片的鏈接外包上一層 ?require()
?,將圖片 URL 轉換為 webpack 模塊請求,并結合 file-loader 或者 url-loader 進行處理。
<!-- 錯誤寫法 -->
<van-image src="./image.png" />
<!-- 正確寫法 -->
<van-image :src="require('./image.png')" />
對此更詳細的解釋可以參考 vue-loader 的處理資源路徑章節(jié)。
使用 Image 組件時,可能會遇到將 <image> 作為標簽名時無法渲染的問題,比如下面的寫法:
<template>
<image src="xxx" />
</template>
<script>
import { Image } from 'vant';
export default {
components: {
Image,
},
};
<script>
這是因為 <image> 標簽是原生的 SVG 標簽,Vue 不允許將原生標簽名注冊為組件名,使用 <van-image> 即可規(guī)避這個問題。
更多建議: