1. 封装全局指令
import { useIntersectionObserver } from "@vueuse/core";
export const lazyPlugin = {
install(app) {
app.directive("img-lazy", {
mounted(el, binding) {
console.log(el, binding.value);
const { stop } = useIntersectionObserver(el, ([{ isIntersecting }]) => {
console.log(isIntersecting);
if (isIntersecting) {
el.src = binding.value;
stop();
}
});
},
});
},
};
2. 注册全局指令
import { lazyPlugin } from "@/directives";
app.use(lazyPlugin);
3.修改HomeHot
<template>
<HomePanel title="人气推荐" sub-title="人气爆款 不容错过">
<template #main>
<ul class="goods-list">
<li v-for="item in hotList" :key="item.id">
<RouterLink to="/">
<img :src="item.picture" alt="" />
<p class="name">{{ item.title }}</p>
<p class="desc">{{ item.alt }}</p>
</RouterLink>
</li>
</ul>
</template>
</HomePanel>
</template>