I have an issue using intersect observer with vue3. I want to have the intersect shown and being activated with a v-if. Unfortunately it only works if I write the intersect Observer code in the onUpdated method. If I write it in the onMounted the method addImages() will not fire anymore.
Can you tell me if it is correct to write it in onUpdated? Will that cause that a lot of the same Intersection Observer Objects will be created every time the component updates?
Or should I write the "new Intersection Observer" outside of the onMounted so it doesnt create always new instances?
onUpdated(() => {
const sections = document.querySelectorAll(".intersectObserver");
const options = {
threshold: 1,
};
let observer = null;
setTimeout(() => {
observer = new IntersectionObserver(function (entries, observer) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
console.log(entry.target);
addImages();
}
});
}, options);
sections.forEach((section) => {
observer.observe(section);
});
}, 2000);
in the template
<div v-if="activateIntersect == true" class="intersectObserver w-full"></div>