浅谈vue-lazyload实现的详细过程
网络编程 2021-07-04 18:33www.168986.cn编程入门
本篇文章主要介绍了浅谈vue-lazyload实现的详细过程,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
本文介绍了浅谈vue-lazyload实现的详细过程,分享给大家,也给自己留个笔记
首先 ,在命令行输入npm install vue-lazyload
&&pm install vue-lazyload
然后,在main.js里引入这个模块。
import 'VueLazyload' from 'vue-lazyload' Vue.use(VueLazyload,{ preload:1.3,//预加载的宽高 loading:"img的加载中的显示的图片的路径", error:"img加载失败时现实的图片的路径", attempt:3,//尝试加载的次数 listenEvents:['scroll','wheel','mousewheel','resize','animationend','transitionend','touchmove'], //你想让vue监听的事件 })
然后在app.vue的template里写一个
<img v-lazy="img.src"/>
然后在app.vue的script里写
data(){ return { img:{ src:"图片的真是路径" } } }
捋一下思路:
//main.js // The Vue build version to load with the `import` mand // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import $ from 'jquery' import 'assets/bootstrap/css/bootstrap.min.css' import 'assets/bootstrap/js/bootstrap.min' import router from '@/router/index' import VueLazyload from 'vue-lazyload' Vue.use(VueLazyload,{ preload:1.3, loading:require('../static/imgs/ad3.png'), //解释一下为什么是require('.....url'):因为vue自带webpack打包工具,如果是图片路径就会把他当成模块解析,所以直接引入就好了。 //记得把里面的路径换成自己的哦 listenEvents:['mousewheel'], }) //载入vue-router //import Vue from 'vue' /* eslint-disable no-new */ new Vue({ el: '#app', router, template: '<App/>', ponents: { App } }) //app.vue <template> <div id="app"> <navbar></navbar> <router-view></router-view> <hello></hello> <ul> <li v-for="item in imgUrl"> <img v-lazy="item.src" alt="" width="300" height="150"/> </li> </ul> <img v-lazy='img[0].src'/> </div> </template> <script> import hello from './ponents/Hello' import Navbar from '@/ponents/navBar' import route from '@/ponents/route' export default { name: 'app', ponents:{ hello, Navbar }, data() { return { imgUrl: [ {src: require('@/assets/imgs/ad1.png')},//记得把里面的路径换成自己的哦 {src: require('@/assets/imgs/ad1.png')},//记得把里面的路径换成自己的哦 ], img:[ {src:require('@/assets/imgs/ad2.png')}//记得把里面的路径换成自己的哦 ] } } } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-: 60px; } </style>
这只是一个简单的vue-lazyload的实现,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程