打字稿电子渲染器过程无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打字稿电子渲染器过程无法正常工作相关的知识,希望对你有一定的参考价值。

我正在使用电子团队here提供的这个样板。主要过程正在运行,但是当我使用渲染器进程创建一个新窗口时,它会出现此错误

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

当我搜索它是因为.remote没有从编译的打字稿到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();
});

当我试图不将打字稿代码编译为javascript并直接使用.remote运行时,它可以工作。那么打字稿代码怎么办?

答案

我遇到了同样的问题。

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

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

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

瞧!有效。

我不知道为什么,但是没有一个电子打字机指南正在工作,因为渲染器没有被编译和需要动态,并且Github上有一个未解决的问题(已存在数月)而没有回复。嗯,这是我的解决方法。我不知道这会造成任何负面影响。

以上是关于打字稿电子渲染器过程无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

Babel 模块解析器无法按预期工作(节点 + 打字稿)

迁移到打字稿后,Firebase 云功能无法正常工作

markdown 打字稿...编码说明,提示,作弊,指南,代码片段和教程文章

如何将 C# 正则表达式转换为打字稿正则表达式?

Aurelia:使用 es6 导入电子 + 打字稿

在打字稿中检查 NAN [重复]