使用 vant 的 v-lazy 实现图片 vue 在移动端的懒加载


官方文档:https://youzan.github.io/vant/#/zh-CN/lazyload

引入

Lazyload 是 Vue 指令,使用前需要对指令进行注册

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

// options 为可选参数,无则不传
Vue.use(Lazyload, options);

基本用法

将v-lazy指令的值设置为你需要懒加载的图片

<img v-for="img in imageList" v-lazy="img" >
export default {
  data() {
    return {
      imageList: [
        'https://img.yzcdn.cn/vant/apple-1.jpg',
        'https://img.yzcdn.cn/vant/apple-2.jpg'
      ]
    };
  }
}

背景图懒加载

和图片懒加载不同,背景图懒加载需要使用 v-lazy:background-image,值设置为背景图片的地址,需要注意的是必须声明容器高度。

<div v-for="img in imageList" v-lazy:background-image="img" />

组件懒加载

将需要懒加载的组件放在 lazy-component 标签中,即可实现组件懒加载

// 注册时设置`lazyComponent`选项
Vue.use(Lazyload, {
  lazyComponent: true
});
<lazy-component>
  <img v-for="img in imageList" v-lazy="img" >
</lazy-component>

API


更多内容请参照:vue-lazyload 官方文档


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM