Javascript 未捕获引用错误:未定义要求-Electron js
我第一次使用electron,同时使用js和node,我试图制作一个嗜好和知识的应用程序。 在实现打开外部链接的脚本时,我遇到了一个问题 我一直在使用来解决这个问题,但我没有看到任何东西可以帮助我解决这个问题 我的ex-links.jsJavascript 未捕获引用错误:未定义要求-Electron js,javascript,html,electron,Javascript,Html,Electron,我第一次使用electron,同时使用js和node,我试图制作一个嗜好和知识的应用程序。 在实现打开外部链接的脚本时,我遇到了一个问题 我一直在使用来解决这个问题,但我没有看到任何东西可以帮助我解决这个问题 我的ex-links.js const electron=require(“电子”); 常数{shell}=电子; const exLinkBtn=document.getElementById(“open ex link”); exLinkBtn.addEventListener(“单击
const electron=require(“电子”);
常数{shell}=电子;
const exLinkBtn=document.getElementById(“open ex link”);
exLinkBtn.addEventListener(“单击”,函数(){
shell.openExternal(“https://www.youtube.com/watch?v=ifXalt3MJtM");
});
我已经尝试将slider.html放在index.html中,但仍然会出现这个错误,所以我将它放在了iframe中
我希望通过单击open ex link按钮,它将在默认浏览器中打开链接。默认的
节点集成
值为false
。因此,您可以将其设置为true
,以解决该问题
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
});
但是,当您在应用程序上执行一些不受信任的远程代码时,nodeIntegration:true
是一种安全风险。例如,当您的应用程序打开第三方网页时,这将是一个安全风险,因为第三方网页可以访问节点运行时,并且可以在用户的文件系统上运行恶意代码。因此设置nodeIntegration:false是有意义的,这是现在的默认设置
您可以使用预加载脚本,因为它们可以访问require
和其他Node.js功能
index.js类似于:
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(app.getAppPath(), 'preload.js')
}
})
const { remote } = require('electron');
let currentWindow = remote.BrowserWindow.getFocusedWindow();
window.closeCurrentWindow = function(){
currentWindow.close();
}
preload.js类似于:
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(app.getAppPath(), 'preload.js')
}
})
const { remote } = require('electron');
let currentWindow = remote.BrowserWindow.getFocusedWindow();
window.closeCurrentWindow = function(){
currentWindow.close();
}
我觉得奇怪的第一件事是,这看起来像你的客户端应用程序。在这种情况下,我不能100%确定require
是否能在浏览器中工作。您是否尝试过改用import
?请查看以下内容:too@mwilson我试着让它只是在客户端,它会影响吗,这是我的可能的副本