简体   繁体   中英

Typescript Electron Renderer process not working

I'm using this boilerplate provided by electron team here . The main process is working but when I use renderer process to create a new window it gives this error

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

As I searched it is because of .remote is absent from the compiled typescript to javascript. Code is

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();
});

When I tried to not compile the typescript code to javascript & directly run with .remote it works. So how to do with typescript code?

I was experiencing this same issue.

When installing the quick start with npm and starting it, a folder called "Dist" was created. There is a renderer.js file in this folder. This is where the typescript renderer file is being compiled into JS.


In my index.html, I changed

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

And voila! It worked.

I am not sure why, but none of the Electron Typescript Guides are working because of the renderer.ts not being compiled and required on the fly, and there is an open issue on Github (has been there for months) with no response. Well, this is my workaround. I am not aware of any negative implications this would cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM