[Vuejs]-How to use Intersection Observer API immediately?

-1👍

its better to be in mounted lifecycle hook because you are using refs, and that means onMounted in composition api. but the is in your callback and here it is:

    const ready = ref(false);
    const myRef = ref(null);

   onMounted(() => {
  const vm = getCurrentInstance();
  let options = {
   root: vm.$refs.myRef
   rootMargin: '0px',
   threshold: 1.0
  }

   let observer = new IntersectionObserver(([entry]) => {
      if (entry && entry.isIntersecting) {
        ready.value = true;
      }
    }, options);
     observer.observe(myRef.value)
 })

  return { ready, myRef }
👤TEFO

Leave a comment