简体   繁体   English

Eslint - 在定义之前使用了“观察者”?

[英]Eslint - 'observer' was used before it was defined?

I am trying to solve my order issue I am getting an Eslint error where "observer" was used before it was defined.我正在尝试解决我的订单问题我收到一个 Eslint 错误,其中在定义之前使用了“观察者”。 But when I replace the observer above the onInterSection function I get onIntersection was used before it was defined.但是,当我替换 onInterSection function 上方的观察者时,我得到 onIntersection 在定义之前已使用。

(() => {
    let images = [...document.querySelectorAll('[data-src]')];

    const settings = {
        rootMargin: '50px 0px',
        threshold: 0.01
    };

    const onIntersection = (imageEntites) => {
        imageEntites.forEach((image) => {
            if (image.isIntersecting) {
                observer.unobserve(image.target);
                image.target.src = image.target.dataset.src;
                image.target.onload = () =>
                    image.target.classList.add('loaded');
            }
        });
    };

    let observer = new IntersectionObserver(onIntersection, settings);

    images.forEach((image) => observer.observe(image));
})();

You could try and write it like this:你可以试着这样写:


    let observer = new IntersectionObserver((imageEntites) => {
        imageEntites.forEach((image) => {
            if (image.isIntersecting) {
                observer.unobserve(image.target);
                image.target.src = image.target.dataset.src;
                image.target.onload = () =>
                    image.target.classList.add('loaded');
            }
        });
    };, settings);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM