javascript - typescript Electron 渲染器进程不工作

标签 javascript typescript electron

我正在使用 Electron 团队提供的样板 here .主进程正在运行,但是当我使用渲染器进程创建一个新窗口时,它给出了这个错误

Uncaught TypeError: electron_1.BrowserWindow is not a constructor
at HTMLButtonElement

正如我所搜索的那样,这是因为 .remote 在编译的 typescript 到 javascript 中不存在。代码是

main.ts:

import { app, BrowserWindow } from "electron";
import * as path from "path";

let mainWindow: Electron.BrowserWindow;

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    height: 600,
    width: 800,
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, "../index.html"));

  // Open the DevTools.
  // mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on("closed", () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow);

// Quit when all windows are closed.
app.on("window-all-closed", () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  // On OS X it"s common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow();
  }
});

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.

renderer.ts:

// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
import { BrowserWindow } from "electron";
import * as path from "path";


let childWindow: Electron.BrowserWindow;

const newWindowBtn = document.getElementById('new-window');

newWindowBtn.addEventListener('click', (event) => {
    const modalPath = path.join('file://', __dirname, '../modal.html');
    childWindow = new BrowserWindow({ width: 400, height: 320 });

    childWindow.on('close', () => { childWindow = null; });
    childWindow.loadURL(modalPath);
    childWindow.show();
});

当我尝试不将 typescript 代码编译为 javascript 并直接使用 .remote 运行时,它起作用了。 那么如何处理 typescript 代码呢?

最佳答案

我遇到了同样的问题。

使用 npm 安装快速入门并启动它时,会创建一个名为“Dist”的文件夹。该文件夹中有一个 renderer.js 文件。这是将 TypeScript 渲染器文件编译成 JS 的地方。


在我的 index.html 中,我改变了

<script> require('./src/renderer.ts') </script>

<script> require('./dist/renderer.js') </script>

瞧!有效。

我不确定为什么,但是没有一个 Electron Typescript Guides 能正常工作,因为 renderer.ts 没有被编译,并且在运行时需要,并且 Github 上有一个 Unresolved 问题(已经存在几个月了)没有回复。 好吧,这是我的解决方法。我不知道这会导致任何负面影响。

关于javascript - typescript Electron 渲染器进程不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782983/

相关文章:

javascript - 有条件地将 Electron 导入 native Web应用程序

javascript - 我们如何将消息从主进程发送到 Electron 中的渲染进程

javascript - 如何设置带时区的倒计时?

javascript - 如何在文本框中输入文本后显示按钮?

javascript - app.js 文件在被编辑后停止工作

visual-studio - 有没有办法将 typescript 定义添加到 "window"对象

typescript - 是否应该创建一个名为 _this 的变量与此冲突?

angular - 如何根据路由参数指定要调用的方法?

javascript - Ionic Accordion 中断的切换功能。不展开第二个 Accordion 。(传递的数据太多?)

google-chrome - Electron 如何确定自定义协议(protocol)的来源