【问题标题】:Lazyload polyfill / Magneto 2 js error "loading-attribute-polyfill.js Uncaught ReferenceError: module is not defined"Lazyload polyfill / Magneto 2 js 错误“loading-attribute-polyfill.js Uncaught ReferenceError: module is not defined”
【发布时间】:2021-04-26 15:56:32
【问题描述】:

我正在尝试将 this lazyload polyfill script 加载到我的 Magneto 2 项目中。

代码有效,但我正在尝试修复控制台错误但没有运气。

Chrome 控制台: loading-attribute-polyfill.js Uncaught ReferenceError: module is not defined 在加载属性-polyfill.js:263

Safari 控制台: ReferenceError:找不到变量:模块

我尝试通过 xml 将其加载到头部,在 phtml 模板结束之前手动加载,也通过 requirejs-config.js 加载,都给出相同的错误。

如果重要的话,我会像这样通过 requirejs 加载它:

var config = {
    deps: [
      "js/vendor/loading-attribute-polyfill"
    ],
    map: { ... } 
};

有没有人将此脚本实现到 magento 2 中而没有出现此错误?我可以对脚本进行手动编辑以解决此问题吗?

干杯!!

【问题讨论】:

  • 您要使用该 polyfil 的哪个版本?有很多版本github.com/mfranzke/loading-attribute-polyfill/tree/master/distUMD 看起来兼容RequireJS
  • @DamianDziaduch 我使用的是“正常”版本(这是唯一真正有效的版本)。我确实尝试了 UMD,但这破坏了功能(图像不会从它们的“noscript”包装器中移出),并且我得到一个“不匹配的匿名定义()模块:”。我尝试通过requirejs加载它,在requirejs-config.js中使用'deps',以及上面提到的其他方式。 'module' 版本给出了“Unexpected token 'export'”错误。据我所知,除了这个控制台错误之外,脚本似乎工作得很好,但我仍然需要阻止它发生!
  • @DamianDziaduch 如果您将其放在答案中,您可以获得赏金(我无法发表评论)

标签: javascript requirejs lazy-loading magento2 polyfills


【解决方案1】:

您要使用该 polyfil 的哪个版本?有很多版本https://github.com/mfranzke/loading-attribute-polyfill/tree/master/dist

UMD 看起来与 RequireJS 兼容,但可能需要更改配置。

【讨论】:

    【解决方案2】:

    @DamianDziaduch 回复您的评论后,我尝试通过 requireconfig.js 'deps' 再次使用 UMD 版本,它确实有效,控制台错误消失了,一定是缓存或我之前尝试过的东西?‍♀️感谢提示! 现在我知道在 magento2/requirejs 中尽可能使用 UMD javascript(我在谷歌搜索时没有遇到这个信息!)???

    【讨论】:

    • 我将添加答案,以便您可以给我赏金:)
    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2020-12-24
    • 2017-02-25
    • 2023-03-30
    • 1970-01-01
    • 2017-08-06
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多