Home  >  Article  >  Web Front-end  >  How to use lazy loading in vue.js

How to use lazy loading in vue.js

coldplay.xixi
coldplay.xixiOriginal
2020-11-11 14:13:123764browse

vue.js uses the lazy loading method: first install lazyload; then reference it globally in [main.js] and configure the image; finally, bind the image that needs lazy loading to the vue file [v-bind] :src] was changed to [v-lazy].

How to use lazy loading in vue.js

The operating environment of this tutorial: windows10 system, vue2.9, this article is applicable to all brands of computers.

[Related article recommendations: vue.js]

vue.js uses lazy loading method:

1. Install lazyload

npm install lazyload -s

2. Reference it globally in main.js and configure the image

import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload) //默认配置
// 或者是自己配置预加载图片
Vue.use(VueLazyload, {
  preLoad: 1.3,
  error: 'dist/error.png',
  loading: 'dist/loading.gif',
  attempt: 1
})

3. Bind the image that needs lazy loading to v-bind:src in the vue file Change it to v-lazy

<img v-lazy="&#39;@/assets/images/27.png&#39;" class="images-line">

4. Does lazyload fail to reference images with relative paths? Operation require reference picture

<img v-lazy="require(&#39;@/assets/images/eight.png&#39;)" class="eight-top">

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to use lazy loading in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn