问题
I'm trying to add jquery functionality to a desktop app written in electron
Using the electron-quick-start repo i'm adding the downloaded jquery file to the main.html
file like so:
<script> require("./jquery.min.js"); </script>
or so:
<script>window.$ = window.jQuery = require('./jquery.min.js');</script>
Then in the index.js
file i'm adding code in the createWindow
function, since that seems the proper place, but to be honest any place i try gets me the same error more or less.
mainWindow.$
is undefined
and the same goes for BrowserWindow
and app
mainWindow
is defined inside the createWindow
function like so:
mainWindow = new BrowserWindow({width: 800, height: 600})
and BrowserWindow is declared on top of the file like so:
const BrowserWindow = electron.BrowserWindow
Any idea where i'm going wrong, what declarations i should change/add?
Thanks in advance
回答1:
While using electron, some additional symbols are also inserted into DOM causing problems. So, you can use jquery as follow
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js" onload="window.$ = window.jQuery = module.exports;"></script>
Notice the code inside "onload".
回答2:
When you call require
inside index.js
, or the main process, it's looking for the node module. So you'll need to install jQuery
via npm and save it as a dependency in your apps package.json file.
npm install jquery --save
Then your index.js
should theoretically see it just fine using
let $ = require('jquery');
mainWindow.$ = $;
Refer to the Node.JS section for installing jQuery. This is what Electron uses.
--
OLD ANSWER
Inside your main.html
, just include the JavaScript like you would any traditional JS file.
<script src="./jquery.min.js"></script>
来源:https://stackoverflow.com/questions/37745273/integrate-jquery-into-a-electron-app