Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vant Image picture


May 07, 2021 Vant


Table of contents


Introduced

Enhanced img tab with a variety of picture fill modes that support lazy, in-load, and fail-loading prompts

Introduced

import Vue from 'vue';
import { Image } from 'vant';

Vue.use(Image);

Code demo

Basic usage

The underlying usage is consistent with the native img label, and native properties such as src, width, height, alt can be set

<van-image
  width="100"
  height="100"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

Fill mode

The fit property allows you to set the picture fill mode, with optional values in the table below

<van-image
  width="10rem"
  height="10rem"
  fit="contain"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

Round picture

The rund property allows you to circle a picture, noting that a complete circle cannot be filled when the picture is uneconciable in width and fit is contain or scale-down.

<van-image
  round
  width="10rem"
  height="10rem"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

The picture is lazy to load

Setting the lazy-load property to turn on picture lazy loading needs to be used with the Lazyload component

<van-image
  width="100"
  height="100"
  lazy-load
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>
import Vue from 'vue';
import { Lazyload } from 'vant';

Vue.use(Lazyload);

Hints in the load

The Image component provides default loading prompts that support custom content through the loading slot

<van-image src="https://img.yzcdn.cn/vant/cat.jpeg" 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>

Loading failure prompt

The Image component provides the default load failure prompt and supports custom content through the error slot

<van-image src="https://img.yzcdn.cn/vant/cat.jpeg" 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>

Api

Props

Parameters Description Type The default
Src Photo link string -
fit Picture fill mode string fill
Alt Alternate text string -
width Width, the default unit is px number | string -
height Height, the default unit px number | string -
radius v2.1.6 Fillet size, the default unit is px number | string 0
round Whether to appear as a circle boolean false
lazy-load Whether to turn on lazy loading of pictures should be used in conjunction with the Lazyload component boolean false
show-error v2.0.9 Whether to show a picture loading failure prompt boolean true
show-loading v2.0.9 Whether to display the prompts in the picture load boolean true
error-icon v2.4.2 The icon name or picture link prompted when it fails string warning-o
loading-icon v2.4.2 The name of the icon or picture link prompted when loading string photo-o

Image padding mode

Name Meaning
contain Keep the picture wide and wide so that the long edges of the picture are fully displayed
cover Keep the picture wide and wide so that the short edges of the picture are fully displayed and the long edges are cropped
fill Stretch the picture so that it fills with elements
none Keep the original size of the picture
scale-down Take none contain in none or contain

Events

The name of the event Description Callback parameters
Click Triggered when you click on a picture event: Event
load Triggered when the picture is loaded -
error Triggered when the picture fails to load -

Slots

Name Description
loading Customize the prompt content in the load
error Custom the prompt content when the load fails


Example demonstration