java jquery的定义方法,电子:jQuery没有定义

回答(16)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

A better and more generic solution IMO:

Benefits

适用于浏览器和电子,代码相同

修复了所有第三方库(不仅仅是jQuery)的问题,而无需指定每个库

Script Build / Pack Friendly(即Grunt / Gulp所有脚本到vendor.js)

不要求 node-integration 为假

e15298c6a3b4591803e154ab0c3b3e2e.png

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 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

编写 的另一种方法是:

e15298c6a3b4591803e154ab0c3b3e2e.png

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;

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

刚遇到同样的问题

npm install jquery --save

为我工作

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

您可以在BrowserWindow上放置 node-integration: false 选项 .

例如: window = new BrowserWindow({'node-integration': false});

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

A nice and clean solution

使用npm安装jQuery . ( npm install jquery --save )

使用它:

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我想我理解你的斗争我解决了一点点不同 . 我使用脚本加载器为我的js文件,其中包括jquery.Script加载器获取你的js文件并将它附加到你的vendor.js文件的顶部它为我做了魔术 .

安装脚本加载程序后,将其添加到引导或应用程序文件中 .

import'script!path / your-file.js';

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

好的,这是另一个选择,如果你想要一个亲戚包括......

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

delete window.module;

在你的jquery导入之前,你很高兴 . more info here .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

使用以下代码为我工作

var $ = require('jquery')

e15298c6a3b4591803e154ab0c3b3e2e.png

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",

...

...

]

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

1.使用npm安装jQuery .

npm install jquery --save

2 .

if (typeof jQuery == "undefined"){window.$ = window.jQuery = require('jquery');}

e15298c6a3b4591803e154ab0c3b3e2e.png

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');

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我面临同样的问题,这对我有用!

使用 npm 安装jQuery

npm i jquery

然后以下列方式之一包含jQuery .

使用脚本标记

使用巴别塔

import $ from "jquery";

使用Webpack

var $ = require('jquery')

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

这适合我 .

if (typeof module === 'object') {window.module = module; module = undefined;}

需要考虑的事项:

1)在 之前将其放入部分

2)在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值