回答(16)
2 years ago
A better and more generic solution IMO:
Benefits
适用于浏览器和电子,代码相同
修复了所有第三方库(不仅仅是jQuery)的问题,而无需指定每个库
Script Build / Pack Friendly(即Grunt / Gulp所有脚本到vendor.js)
不要求 node-integration 为假
2 years ago
if ( typeof module === "object" && typeof module.exports === "object" ) {
// set jQuery in `module`
} else {
// set jQuery in `window`
}
jQuery代码"sees",它在CommonJS环境中运行并忽略 window .
The solution is really easy ,而不是通过
Note: 需要路径前的点,因为它表示它是当前目录 . 另外,请记住在加载依赖于它的任何其他插件之前加载jQuery .
2 years ago
编写 的另一种方法是:
2 years ago
电子FAQ回答:
我不能在Electron中使用jQuery / RequireJS / Meteor / AngularJS . 由于Electron的Node.js集成,有一些额外的符号插入到DOM中,如module,exports,require . 这会导致某些库出现问题,因为它们希望插入具有相同名称的符号 . 要解决此问题,您可以在Electron中关闭节点集成://在主过程中 .
let win = new BrowserWindow({
webPreferences: {
nodeIntegration: false } });
但是如果你想保留使用Node.js和Electron API的能力,你必须在包含其他库之前重命名页面中的符号:
window.nodeRequire = require;
delete window.require;
delete window.exports; delete window.module;
2 years ago
刚遇到同样的问题
npm install jquery --save
为我工作
2 years ago
您可以在BrowserWindow上放置 node-integration: false 选项 .
例如: window = new BrowserWindow({'node-integration': false});
2 years ago
A nice and clean solution
使用npm安装jQuery . ( npm install jquery --save )
使用它:
2 years ago
我想我理解你的斗争我解决了一点点不同 . 我使用脚本加载器为我的js文件,其中包括jquery.Script加载器获取你的js文件并将它附加到你的vendor.js文件的顶部它为我做了魔术 .
安装脚本加载程序后,将其添加到引导或应用程序文件中 .
import'script!path / your-file.js';
2 years ago
好的,这是另一个选择,如果你想要一个亲戚包括......
2 years ago
delete window.module;
在你的jquery导入之前,你很高兴 . more info here .
2 years ago
使用以下代码为我工作
var $ = require('jquery')
2 years ago
如果您使用的是Angular2,则可以使用以下代码创建新的js文件:
// jquery-electron.js
if ((!window.jQuery || !window.$) && (!!module && !!module.exports)) {
window.jQuery = window.$ = module.exports;
}
并在jangle路径之后将其放在.angular-cli.json中:
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"assets/js/jquery-electron.js",
...
...
]
2 years ago
1.使用npm安装jQuery .
npm install jquery --save
2 .
if (typeof jQuery == "undefined"){window.$ = window.jQuery = require('jquery');}
2 years ago
im building和Angular App with electron,我的解决方案如下:
index.html
if ( typeof module === "object" && typeof module.exports === "object" ) {
window.$ = window.jQuery = require('jquery');
}
angular.json
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/popper.js/dist/umd/popper.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
因此,如果在浏览器上,Jquery将从angular.json加载,否则如果它是电子构建应用程序则需要模块 .
如果要在index.html中导入jquery而不是从angular.json导入,请使用以下解决方案:
if ( typeof module === "object" && typeof module.exports === "object" ) {
window.$ = window.jQuery = require('jquery');
}
2 years ago
我面临同样的问题,这对我有用!
使用 npm 安装jQuery
npm i jquery
然后以下列方式之一包含jQuery .
使用脚本标记
使用巴别塔
import $ from "jquery";
使用Webpack
var $ = require('jquery')
2 years ago
这适合我 .
if (typeof module === 'object') {window.module = module; module = undefined;}
需要考虑的事项:
1)在 之前将其放入部分
2)在