【问题标题】:How to open system browser from electron container如何从电子容器打开系统浏览器
【发布时间】:2021-05-20 15:23:30
【问题描述】:

我已经创建了反应应用程序,之后我添加了电子模块来反应应用程序。实际上我正在尝试从具有反应应用程序的电子容器打开操作系统浏览器。我正在使用最新的电子版本 (12)。我试了很多方法,但我无法弄清楚。

代码:

const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const isDev = require("electron-is-dev");

const path = require("path");

let mainWindow;

function createWindow() {
  mainWindow  = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule:true,
    }
  })
  mainWindow.loadURL("http://localhost:3000");
  mainWindow.on("closed", () => {
    mainWindow = null;
  });
}

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (mainWindow === null) {
    createWindow();
  }
});
import React from "react";
const electron = window.require("electron");

function App() {
  const openBrowser = () => {};

  return <div onClick={openBrowser}>{isRenderer ? "Electron" : "react"}</div>;
}

export default App;


×
TypeError: window.require is not a function
Module.<anonymous>
src/App.tsx:3
  1 | import React from "react";
  2 | var isRenderer = require("is-electron-renderer");
> 3 | const electron = window.require("electron");
  4 | 
  5 | function App() {
  6 |   const openBrowser = () => {};

【问题讨论】:

    标签: reactjs electron


    【解决方案1】:

    您是否尝试过以下方法?我用它来打开新的浏览器窗口。

    const googleLink = `https://www.google.com`
    window.open(googleLink, ‘_blank’)
    

    【讨论】:

    • TypeError: window.require is not a function Module.,我收到此错误
    • 我正在尝试从电子操作系统浏览器中打开 url
    • 除非您使用来自电子的任何东西,否则您不需要该行。 Electron 本身是一个 chrome 浏览器,使用window.open 足以打开一个新窗口
    • 我有这样的要求,我需要在反应代码中访问电子外壳你知道吗?
    • 请编辑问题标题和问题帖子以反映您的问题,因为它与打开新窗口无关。之后我可以给你答复。
    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多