百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
V

vue3-lazyload

> 前端框架
开源

一个 Vue3.x 图片懒加载插件

190 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个 Vue3.x 图片懒加载插件

vue3-lazyload

A Vue 3 lazyload plugin for images and component subtrees.

Features

  • ⚡ 0 runtime dependencies: No worry about your bundle size
  • Type Strong: Written in Typescript
  • Vue 3 only: Built for Vue 3 applications and libraries
  • Browser support: Use it through CDN
  • Support Hook: useLazyload
  • Support LazyComponent: Defer mounting slot content until it enters the viewport
  • Support modes and events: Keep content mounted once loaded, or mount/unmount with visibility events

Installation

bash
$ npm i vue3-lazyload
# or
$ yarn add vue3-lazyload
# or
$ pnpm add vue3-lazyload

CDN

CDN: https://unpkg.com/vue3-lazyload

xml

Usage

This package supports Vue 3 only.

main.js:

javascript
import { createApp } from 'vue'
import VueLazyLoad from 'vue3-lazyload'
import App from './App.vue'

const app = createApp(App)
app.use(VueLazyLoad, {
  // options...
})
app.mount('#app')

App.vue:

xml

  

Which API Should I Use?

API Best for What it controls
v-lazy Regular template images Swaps image src when the element becomes visible
useLazyload Images managed inside component logic Reuses plugin defaults and lets the component drive source updates
LazyComponent Expensive cards, charts, or nested subtrees Delays mounting a whole slot subtree

v-lazy and useLazyload are image-oriented. LazyComponent is for component-level lazy mounting.

v-lazy use object params

xml

  

Use lifecycle

In main.js

javascript
import { createApp } from 'vue'
import VueLazyLoad from 'vue3-lazyload'
import App from './App.vue'

const app = createApp(App)
app.use(VueLazyLoad, {
  loading: '',
  error: '',
  lifecycle: {
    loading: (el) => {
      console.log('loading', el)
    },
    error: (el) => {
      console.log('error', el)
    },
    loaded: (el) => {
      console.log('loaded', el)
    }
  }
})
app.mount('#app')

or

In xxx.vue

Do not pass a whole reactive object as v-lazy="lazyOptions". Use inline object fields instead, otherwise Vue cannot track nested changes the way you expect.

xml

  

Use Hook

xml

  
  
    Switch source
  

If you install the plugin with app.use(VueLazyLoad, options), useLazyload() will reuse the injected lazyload instance by default. If you pass local options to the hook, they are merged on top of the plugin defaults without mutating the global instance.

Use LazyComponent

Use LazyComponent when the expensive part is not an image but a whole component subtree.

xml

  
    
      

    

    
  

LazyComponent supports two modes:

  • mode="once": mount the slot the first time it becomes visible, then keep it mounted
  • mode="visible": mount on enter, unmount on leave, and restore the placeholder

Example with visibility events:

xml

  
    
      

    

    
  

  
Visible: {{ visible }}

  
Mounts: {{ mounts }}

  
Unmounts: {{ unmounts }}

LazyComponent is SSR-safe by default: the server render starts from the placeholder and the real slot content mounts only after client-side visibility is known.

LazyComponent Props

prop description default type
mode Mount strategy for the default slot 'once' 'once' | 'visible'
delay Time in milliseconds the component must remain visible before mounting inherited from plugin options number
observerOptions IntersectionObserver options for the wrapper inherited rootMargin/threshold defaults IntersectionObserverInit
tag Root element tag rendered by LazyComponent 'div' string

LazyComponent Events

event payload description
visible-change boolean Fires whenever the wrapper enters or leaves the viewport
load none Fires when the default slot is mounted
unload none Fires when mode="visible" unmounts the default slot

Use css state

There are three states while image loading.
You can take advantage of this feature, make different css controls for different states.

  • loading
  • loaded
  • error
xml
css

Delay loading of images

To avoid loading images that are only shortly visible (e. g. fast scrolling through list of images), a delay in milliseconds can be configured. If a delay is set, an image is only loaded if it stays visible for the specified amount of time.

Set delay in object parameter:

xml

  

Options

key description default type
loading The image used when the image is loaded - string
error The image used when the image failed to load - string
observerOptions IntersectionObserver options { rootMargin: '0px', threshold: 0 } IntersectionObserverInit
log Do print debug info true boolean
logLevel Log level error 'error' | 'warn' | 'info' | 'debug' | 'log'
lifecycle Specify state execution function - Lifecycle
delay Time in milliseconds an image has to stay visible before loading starts 0 number

⛱ Lifecycle Hooks

key description
loading Image loading
loaded Image loaded
error Image load error

Sponsors

Contributors

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptlazyloadpluginvuevue3

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架