【问题标题】:How to use Electron BrowserWindow in React Typescript application如何在 React Typescript 应用程序中使用 Electron BrowserWindow
【发布时间】:2022-07-11 12:31:17
【问题描述】:

我目前正在尝试找到一种将第三方 URL(例如 disney+ 或 spotify)集成到我的应用程序中的方法。我很快发现iframe 是不可能的,因为很多网站都屏蔽了它。我的下一个想法是使用Electron's webview 标签。但是,这已经贬值了。

现在我有了将 Electron 的 BrowserWindow 集成到 React Dom 中的想法。 所以基本上,写一个electron React application

https://medium.com/folkdevelopers/the-ultimate-guide-to-electron-with-react-8df8d73f4c97

如果我按照本指南导入所有内容,我会收到此错误。

App.tsx:2 Uncaught TypeError: window.require is not a function

我知道这是打字稿..需要...不是解决方案。但是导入也不起作用。我还尝试构建一个普通的 javascript 应用程序来测试它,但效果不佳。

我也确信,即使我得到正确的导入,我也应该得到一个constructor error。有没有人有这个问题的经验,可以请帮助我。

电子 main.ts

import { app, BrowserWindow } from 'electron'
import * as path from 'path'
import installExtension, {
  REACT_DEVELOPER_TOOLS,
} from 'electron-devtools-installer'

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      // contextIsolation: false,
      preload: path.join(__dirname, 'preload.js'),
    },
  })

  if (app.isPackaged) {
    // 'build/index.html'
    win.loadURL(`file://${__dirname}/../index.html`)
  } else {
    win.loadURL('http://localhost:3000/index.html')

    win.webContents.openDevTools()

    // Hot Reloading on 'node_modules/.bin/electronPath'
    require('electron-reload')(__dirname, {
      electron: path.join(
        __dirname,
        '..',
        '..',
        'node_modules',
        '.bin',
        'electron' + (process.platform === 'win32' ? '.cmd' : '')
      ),
      forceHardReset: true,
      hardResetMethod: 'exit',
    })
  }
}

app.whenReady().then(() => {
  // DevTools
  installExtension(REACT_DEVELOPER_TOOLS)
    .then((name) => console.log(`Added Extension:  ${name}`))
    .catch((err) => console.log('An error occurred: ', err))

  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })

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

App.tsx

import './App.css'
const electron = window.require('electron')
const remote = electron.remote
const { BrowserWindow } = remote

function App() {
  return (
    <div className='App'>
      <h1>halllo</h1>
      <button
        onClick={() => {
          let win = new BrowserWindow()
          win.loadURL('https://www.electronjs.org/docs/api/remote')
        }}
      >
        Open BrowserWindowss
      </button>
    </div>
  )
}

export default App

反应 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "outDir": "../build", // Output transpiled files to build/electron/
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

【问题讨论】:

    标签: reactjs typescript electron


    【解决方案1】:

    在 main.ts 中:

     webPreferences: {
       nodeIntegration: true,
       contextIsolation: false,
       preload: path.join(__dirname, 'preload.js'),
    },
    

    【讨论】:

      猜你喜欢
      • 2017-12-18
      • 2023-04-05
      • 2021-04-05
      • 2019-07-23
      • 2018-04-29
      • 2019-03-21
      • 2021-07-14
      • 2021-05-23
      相关资源
      最近更新 更多