【发布时间】:2020-01-11 17:37:24
【问题描述】:
我在做什么?
我正在使用交叉点观察器 API 进行延迟加载。
我尝试了什么?
我在一个简单的 HTML 页面中尝试了代码,它运行良好,但是当我在 vue 中使用代码时,图像不会加载(本地图像)。如果我放一个 Http 源图像(在线图像),它也很完美。我认为这是一个 webpack 错误配置。我对吗?我该如何解决?
什么错误?
当我使用本地图像时,代码不起作用,如果只用类似此图像https://images.pexels.com/photos/69817/france-confectionery-raspberry-cake-fruit-69817.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940 代码 WORKS 的其他东西更改该 src,为什么我不能使其与本地图像一起使用?
HTML 和脚本
<template>
<div class="container" id="section3">
<span class="containerTitle">Galeria</span>
<div class="wrapper">
<img v-lazyload data-src="@assets/images/001.jpg" class="card">
</div>
</div>
</template>
<script>
import lazyload from '../directives/lazyload'
export default {
directives:{
lazyload
},
}
</script>
指令
export default{
inserted: el =>{
const options = {
// root:
rootMargin: '0px 0px 0px 0px',
threshold:1
}
var observer = new IntersectionObserver((entries,observer) =>{
entries.forEach(entry => {
if(entry.isIntersecting){
el.src = el.dataset.src
observer.unobserve(el)
console.log('intersecting');
}
})
},options)
observer.observe(el)
}
}
代码图像
文件夹
【问题讨论】:
-
请将代码作为文本而不是图像包含
-
我的错,更新@Phil
-
最简单的方法是将图像放在
public文件夹中(可能在子文件夹中),然后在data-src属性中使用绝对URL。阅读手册 - cli.vuejs.org/guide/… -
我在 /assets 文件夹 @IVOGELOV 中拥有所有图像
-
好吧,也许这篇博文会对你有所帮助 - markus.oberlehner.net/blog/…