【问题标题】:Angular&Electron - Open link in default browserAngular&Electron - 在默认浏览器中打开链接
【发布时间】:2018-10-04 20:48:39
【问题描述】:

我正在尝试在默认浏览器上打开电子应用程序(使用 Angular)的链接,但收到以下错误:

Uncaught TypeError: fs.existsSync is not a function
    at Object.<anonymous> (vendor.bundle.js:160955)
    at Object../node_modules/electron/index.js (vendor.bundle.js:160961)
    at __webpack_require__ (inline.bundle.js:55)
    at Object../src/app/components/issue/issue.component.ts (main.bundle.js:159)
    at __webpack_require__ (inline.bundle.js:55)
    at Object../src/app/app.module.ts (main.bundle.js:75)
    at __webpack_require__ (inline.bundle.js:55)
    at Object../src/main.ts (main.bundle.js:644)
    at __webpack_require__ (inline.bundle.js:55)
    at Object.0 (main.bundle.js:662)

我的 html 组件上的按钮上有一个“(click)="onNavigate()"” 这就是功能:

import { shell } from 'electron';
...      
onNavigate() {
    shell.openExternal("http://www.google.com");
}
...

我不知道我做错了什么,希望这里的人可以帮助我:)

【问题讨论】:

标签: angular electron fs


【解决方案1】:

使用window.require 而不是require 是避免require 之间冲突的一种方法,因为您使用的是webpack,它带来了自己的require。试试:

const shell = window.require('electron');
...      
onNavigate() {
   shell.openExternal("http://www.google.com");
}
...

【讨论】:

  • 它一直在应用程序范围内打开新窗口,所以我用这种方式解决了这个问题: mainWindow.webContents.on('new-window', (event, url) => { event.preventDefault(); electron.shell.openExternal(url); })
  • 这实际上是我在我的应用程序中所做的,在主进程中处理它。我可能应该在答案中添加它作为替代。
猜你喜欢
  • 2018-12-10
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多