简体   繁体   English

如何在打字稿中加载电子模块

[英]how to load electron module in typescript

I try to get the ipcRenderer module from electron in typescript to send informations from the current component to the core and to get informations back to the window (electron-chromium-browser).我尝试从打字稿中的电子获取 ipcRenderer 模块,以将信息从当前组件发送到核心并将信息返回到窗口(电子铬浏览器)。 All I get is a error "Module not found" by transcoding the ts-code to ES5.通过将 ts-code 转码为 ES5,我得到的只是一个错误“找不到模块”。

const ipc = require('electron').ipcRenderer;` const ipc = require('electron').ipcRenderer;`

Update: The Error is switching between the "Module not found" and this one:更新:错误是在“找不到模块”和这个之间切换:

ERROR in ./~/electron/index.js Module build failed: Error: ENOENT, open '/.../node_modules/electron/index.js' @ ./app/components/search/search.ts 12:10-29

That is from the current electron-api .那是来自当前的electron-api I have also tried to use the import syntax from typescript but the result is the same.我也尝试使用 typescript 中的导入语法,但结果是一样的。

Than I tried to use the electron.ipcRenderer module in a ES5-file, loaded/linked directly in the html-file.比我尝试在 ES5 文件中使用 electron.ipcRenderer 模块,直接在 html 文件中加载/链接。

There it worked.在那里它起作用了。 Why?为什么?

Since electron dependency in the browser app is not real, meaning it's not webpacked from node_modules but instead loaded in runtime, the require statement caused errors such as "fs" not found for me.由于浏览器应用程序中的电子依赖不是真实的,这意味着它不是从 node_modules 中 webpacked 而是在运行时加载的,require 语句导致错误,例如我找不到“fs”。

However you can trick the typescript with this:但是,您可以通过以下方式欺骗打字稿:

if (typeof window['require'] !== "undefined") { let electron = window['require']("electron"); let ipcRenderer = electron.ipcRenderer; console.log("ipc renderer", ipcRenderer); }

Also if you are writing a web app, which only is augmented by electron when it's running inside, this is a better way since you don't have to add electron as a dependency to your webapp just when using the communication parts.此外,如果您正在编写一个 Web 应用程序,当它在内部运行时仅由电子增强,这是一种更好的方法,因为您不必仅在使用通信部分时将电子添加为您的 Web 应用程序的依赖项。

Than I tried to use the electron.ipcRenderer module in a ES5-file, loaded/linked directly in the html-file.比我尝试在 ES5 文件中使用 electron.ipcRenderer 模块,直接在 html 文件中加载/链接。

If it works in html but fails in ts it means the error is not in const ipc = require('electron').ipcRenderer;如果它在html 中工作但在ts 中失败,则意味着错误不在const ipc = require('electron').ipcRenderer; . . The error is most likey in the import you have to load your file from html (and not require('electron') ).该错误最可能出现在您必须从 html 加载文件(而不是require('electron') )的导入中。

This solved the problem for me:这为我解决了问题:

You can fix it, just add to the "package.json"你可以修复它,只需添加到“package.json”

"browser": {
"fs": false,
"path": false,
"os": false }

Source: https://github.com/angular/angular-cli/issues/8272#issuecomment-392777980来源: https : //github.com/angular/angular-cli/issues/8272#issuecomment-392777980

你可以用这个来欺骗 ts(肮脏的黑客,但它有效):

const { ipcRenderer } = (window as any).require("electron");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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