问题描述
这是我的文件夹文件夹结构
This is my folder folder structure
您好,我是 electron.js 的新手,我遇到了一个问题,即我无法在 main.js 文件中捕获 jquery 事件.作为解决方案,我创建了一个单独的文件 (events.js)[现在我可以捕获 jquery 事件] 并将其连接到 index.html.所以在我的 event.js 我添加了一个 cron-job(node-cron) 来检查它是否工作,但是当我尝试运行一个项目时,我得到一个错误提示 require is not defined.没有任何导入库,它就可以工作.
Hi im new to electron.js I was facing for an issue where that i cannot capture jquery events in my main.js file. As a solution i created a separate file (events.js)[now i can capture jquery events] and i connect it to index.html. So in my event.js i added a cron-job(node-cron) to check whether it's working or not, but when i try to run a project i get an error saying require is not defined. Without any import library , it worked.
这是我的 index.html
<body>
<div style="margin-top:15px" class="container">
<div class="row">
<div class="col-xs-3">
<a class="btn btn-info btn-sm btn-block" href="./layouts/settings.html" id="menu-btn-settings"
role="button">Settings</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/health.html" id="menu-btn-health"
role="button">System Health</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/abc-now.html" id="menu-btn-abc-now"
role="button">Sync Now</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/abc-customer.html" id="menu-btn-abc-user"
role="button">Sync
Customer</a>
<a class="btn btn-info btn-sm btn-block" href="./layouts/about.html" id="menu-btn-about"
role="button">About</a>
</div>
<div class="col-xs-9">
<div id="alert-msg"></div>
<div id="content"></div>
</div>
</div>
</div>
<!-- Insert this line above script imports -->
<script>if (typeof module === 'object') { window.module = module; module = undefined; }</script>
<script src="./assets/js/jquery-3.4.1.min.js"></script>
<script src="./assets/js/bootstrap.min.js"></script>
//
<script src="./main.js"></script>
<script src="./app/events.js"></script>
<script src="./assets/js/require.js"></script>
<script>if (window.module) module = window.module;</script>
</body>
这是我的 main.js
app.on('ready', function () {
win = new BrowserWindow({});
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: false,
webPreferences: {
nodeIntegration: true,
},
}));
win.on('closed', function () {
app.quit();
});
win.webContents.openDevTools();
});
这是我的 event.js
(function () {
'use strict';
var CronJob = require('cron').CronJob;
var job = new CronJob('* * * * * *', function () {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
job.start();
})();
推荐答案
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})
从 win.loadURL
中删除 webPreferences
并添加到 BrowserWindow
选项.下面是在 BrowserWindow
Remove webPreferences
from win.loadURL
and add to BrowserWindow
option. The below is the sample code to enable the Node api in BrowserWindow
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
这篇关于“未定义要求"当我尝试将 js 文件(带有节点模块)导入到我的主电子 js 文件时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!