LARAVEL 错误 hljs 未在 app_debug=false 中定义

LARAVEL error hljs is not defined in app_debug=false

当我禁用调试栏 (barryvdh/laravel-debugbar),或者我在 .env 中放置 APP_DEBUG=false 而不是 APP_DEBUG=true 时,我的图片库脚本 (baguetteBox) 出现错误.

错误:

Uncaught ReferenceError: hljs is not defined at window.onload

如果我不禁用 app_debug 或调试栏,我不会出错,没关系.. 如果我点击我网站的图片并出现此错误,这是错误并在页面上打开图片(在 url: http://127.0.0.1/website/images/image.png),但如果启用 app_debug 或调试栏,图片打开在一个 "modal" 的 baguetteBox(普通)。

我的错误截图: Uncaught ReferenceError: hljs is not defined at window.onload

当DebugBar打开时,库被DebugBar加载,hljs被定义。当 DebugBar 关闭时,hljs 未定义,因此脚本在 hljs.initHighlighting(); 之后中断。试试这个:

    if ( typeof oldIE === 'undefined' && Object.keys && typeof hljs !== 'undefined') {
        hljs.initHighlighting();
   }

你可以这样使用。

const hljs = require('highlight.js');

document.addEventListener('DOMContentLoaded', (event) => {
    document.querySelectorAll('pre code').forEach((block) => {
      hljs.highlightBlock(block);
    });
});