首页 > 解决方案 > 仅在需要并在某处使用时才包含像 jQuery 这样的库

问题描述

有些东西在我的脑海里,但不知道如何实现它。真的可以执行吗?

jQuery 不包含在整个网站中。当它在某些文件中使用时,它会返回一个$jQuery未定义的错误,这就是我想要做的。我想制作类似try/catchaapp.js/root.js/parent.js/index.js (or any other file.js that is accessible from other js files or we can call it the parent of other js files)的东西,以便获取所有throws 以及 throw 的类型(从孩子抛出)是否与调用它的文件相关jQuery$包含jQuery在调用它的文件中。可能吗?

child.js

$(document).html('test');

它没有被定义,它会引发undefined错误。

在这里(我不知道如何)捕捉错误

index.js(app.js / root.js / parent.js 以及任何其他 js 文件可访问的任何文件)

if(typeof error == 'jquery not defined') {
    window.$ = window.jQuery = require('jquery')
}

然后window.$应该返回给调用者以使用它。

谢谢 :)

标签: javascriptjquery

解决方案


推荐阅读