@nuxt-modules/lazy-load
Version:
Lazy load module for Nuxt 3
19 lines (18 loc) • 607 B
JavaScript
import { computed } from "vue";
import { useNuxtApp, useRuntimeConfig, useState } from "#app";
export const useLazyLoad = () => {
const { $lazyLoad } = useNuxtApp();
const { lazyLoad: lazyLoadOptions } = useRuntimeConfig();
const { selector, options } = lazyLoadOptions;
const observer = useState("lazy-load-observer", () => null);
const init = () => {
const lazyLoadObserver = $lazyLoad(`.${selector}`, options);
lazyLoadObserver.observe();
observer.value = lazyLoadObserver;
return lazyLoadObserver;
};
return {
observer: computed(() => observer.value),
init
};
};