首页 > 解决方案 > Electron-forge:如何将入口点设置为 url 而不是静态 html 文件

问题描述

我使用 electron-forge webpack-typescript 模板生成电子项目。

默认情况下,它会按照 in中index.html的配置加载entryPointspackage.json

...
"plugins": [
  [
    "@electron-forge/plugin-webpack",
    {
      "mainConfig": "./webpack.main.config.js",
      "renderer": {
        "config": "./webpack.renderer.config.js",
        "entryPoints": [
          {
            "name": "main_window",
            "html": "./src/index.html", // <-- HERE
            "js": "./src/renderer.ts",
          }
        ]
      }
    }
  ]
]
...

我想加载角度 urlhttp://localhost:4200作为入口点。

当我更改为 时"html": "http://localhost:4200",,它不起作用。

我试图从中删除该行package.json并将网址硬编码在index.ts

...
  // and load the index.html of the app.
  // mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
  mainWindow.loadURL("http://localhost:4200");
...

这也行不通。如何将条目设置为 url 而不是静态 HTML 文件?

标签: typescriptwebpackelectronelectron-forge

解决方案


只需使用重定向将入口点设为空白 html

<html><head>
<meta http-equiv="refresh" content="2;url=http://example.com" />
<title>Page Moved</title>
</head><body>
<a href="http://www.example.com">continue</a></body>
</html>

这样做的好处是,您甚至可以添加小 javascript 来首先检查目标是否可用,可选择显示错误消息等,如果目标端口由于某种原因不适用于应用等,或者在应用打开之前显示简单的 splahh


推荐阅读