【问题标题】:Electron.js how to use require function in html script tagsElectron.js 如何在 html 脚本标签中使用 require 函数
【发布时间】:2020-05-05 07:03:27
【问题描述】:

你好。

我正在尝试使用 Electron.js 开发应用程序。我有个问题。我需要在 html 中使用脚本标签。但是当我这样做时,我的其他 JavaScript 代码不起作用。我该怎么办?

运行:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Application</title>
</head>
<body>

    <button id="tstbtn">TEST BUTTON</button>

<script>
//const ElectronLIB = require("electron");

let tstbtn = document.querySelector("#tstbtn");
tstbtn.addEventListener("click", () => {
    alert();
})

</script>

</body>
</html>

别跑:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>My Application</title>
</head>
<body>

    <button id="tstbtn">TEST BUTTON</button>

<script>
const ElectronLIB = require("electron");

let tstbtn = document.querySelector("#tstbtn");
tstbtn.addEventListener("click", () => {
    alert();
})

</script>

</body>
</html>

注意:当我稍后调用 require 函数时,点击事件正在运行。

【问题讨论】:

  • 看看这个question - 它可能会有所帮助。

标签: javascript html electron


【解决方案1】:

DevTool 中是否有任何错误信息?喜欢:

Uncaught ReferenceError: require is not defined.

如果有错误,可以在打开时启用nodeIntegration

BrowserWindow, for example 
    splashWindow = new BrowserWindow({
     width: 900,
     height: 600,
     show: true,
     frame: false,
     resizable: false,
     webPreferences: {
      nodeIntegration: true
     }
    });

【讨论】:

    【解决方案2】:

    Electron 需要 node.js。在您的 package.json 项目中,主入口点是您声明的位置:

    const ElectronLIB = require("electron");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-06
      • 2019-01-01
      • 2011-07-24
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多