繁体   English   中英

ESLint No-Undef-BigCommerce的JS问题

[英]ESLint no-undef - js issue with BigCommerce

我已经在通过ESLint运行的.js文件中获得了此代码。 但这会引发关于此行的错误: iFrameResize({

说: error 'iFrameResize' is not defined no-undef

如果我这样定义: const iFrameResize()

我的代码不再起作用,如何使ESLint满意并保持代码正常工作?

export default class Page extends PageManager {

before(next) {
    next();
}

loaded(next) {
    next();
}

after(next) {
    const url = Url.parse(location.href, true);
    const IC_PAGE = '/international-checkout';
    const currentPageUrl = url.pathname;
    if (currentPageUrl.indexOf(IC_PAGE) !== -1 && $('#icForm').length === 1) {
        $(document).ready(() => {
            if ($('#icForm').length === 1) {
                if ($('.GiftStatus') && $('.GiftStatus').val() === '1') {
                    alert('Gift Certificate is not available for international orders. Please remove Gift Certificate from shopping cart before proceeding with International Checkout.');
                    window.parent.location.href = '/cart.php';
                    return false;
                }
                $('.icformfields').each((i, e) => {
                    const currentId = $(e).attr('id');
                    const res = currentId.split('-');
                    const finalId = Number(res[1]) + 1;
                    const finalName = $(e).attr('name') + finalId;
                    $(e.currentTarget).attr('name', finalName);
                });
                document.getElementById('icIframe').src = 'https://www.internationalcheckout.com/cart.php';
                document.getElementById('icForm').submit();
                $('#icIframe').load(() => {
                    $('#icForm').remove();
                    $('#loading').css('display', 'none');
                    $('html, body').animate({
                        scrollTop: $('#icIframe').offset().top,
                    }, 1000);
                    $('#icIframe').fadeIn();
                });
            }
        });
        iFrameResize({
            checkOrigin: false,
            enablePublicMethods: true,
        });
    }
    next();
}

}

我想知道如何在不禁用特定行的错误报告的情况下满足ESLint。

值得一提的是,eslint提供了多种解决方案。 请参阅eslint文档

我建议将以下内容添加到文件顶部。 使用此方法可以定义仅在几个地方使用的全局依赖项:

/* global iFrameResize */

您还可以提供一个数组:

/* global iFrameResize, iFrameManage, etc */

如果您经常使用iFrameResize或依赖jQuery之类的东西,请考虑在.eslintrc文件.eslintrc其定义为全局.eslintrc

"globals": {
    "iFrameManage": true,
}

如果您确定代码可以在iFrameResize()并且可能是由于您使用js文件设置的架构iFrameResize() ,那么您可能只想忽略该错误。 最简单的是

// eslint-disable-line

这将禁用该行的esilnt。

由于此函数定义来自可能将其附加到作为window的全局范围的库,因此从该范围调用它就可以了

window.iFrameResizer()

现在eslint知道您正在调用驻留在window对象上的函数,因此它不会抱怨

暂无
暂无

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

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