javascript - 仅在需要并在某处使用时才包含像 jQuery 这样的库
问题描述
有些东西在我的脑海里,但不知道如何实现它。真的可以执行吗?
jQuery 不包含在整个网站中。当它在某些文件中使用时,它会返回一个$
或jQuery
未定义的错误,这就是我想要做的。我想制作类似try/catch
aapp.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)
的东西,以便获取所有throw
s 以及 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.$
应该返回给调用者以使用它。
谢谢 :)
解决方案
推荐阅读
- nginx - Kestrel vs IIS+Kestrel(反向代理) vs Nginx
- javascript - 通过调用 php 文件动态构建 JavaScript
- python - 如何创建具有满足特定条件的特定实例计数的列?
- php - 使用 PHP 发出 20 个 GET 请求会不会太慢?
- javascript - C:/fakepath 与 DROPBOX 上传有关的问题
- php - 创建具有比较居住状态功能的学费计算器
- r - ggplot用百分比标签填充条形图
- sql - 尝试合并两个不同的查询但没有成功
- c# - 如何在抽象类中实现依赖注入
- node.js - 收到付款后如何执行功能(Sofort)?