这是我第一次使用电子,所以这可能是一些愚蠢的问题,但我没有在网上找到任何关于它的信息。
所以,我把这个放在我的index.html文件中
<head>
<script defer src="generate.js"></script>
</head>
<body>
<label for="role">MY LABEL</label>
<select name="abc" id="abc" class="abc">
<option value="0"></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<button onclick="generate()">Generate</button>
</body>这个在我的generate.js文件里
const ipcRenderer = require('electron').ipcRenderer;
const generate = () => {
ipcRenderer.send('generate', document.querySelector('.abc').value)
}理论上,只要有人点击按钮,.js文件中的.js函数就应该运行,但它不会运行。我尝试了一些测试,并删除了ipcRenderer,函数被高亮显示(意味着它正在使用),并且这个函数可以工作。我该怎么解决这个问题?如果需要其他代码,我将共享它。
编辑:删除ipcRenderer时,我尝试使用alert编写代码
发布于 2022-07-06 10:44:41
经过几次尝试,我解决了这个问题。外部.js文件(在本例中为generate.js)不使用NodeJS操作。要解决这个问题,我必须在index.js上添加两行代码,如下所示。
const createWin = () => {
win = new BrowserWindow({
//these lines fixed the issue
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
win.loadFile('index.html')
}https://stackoverflow.com/questions/72880604
复制相似问题