首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电子BrowserWindow在反应型标应用中的应用

电子BrowserWindow在反应型标应用中的应用
EN

Stack Overflow用户
提问于 2022-05-08 10:52:26
回答 2查看 533关注 0票数 1

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

现在我有了把电子的BrowserWindow集成到反应Dom中的想法。所以基本上,要编写一个electron React application

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

如果我遵循这个指南并导入所有东西,我就会得到这个错误。

代码语言:javascript
复制
App.tsx:2 Uncaught TypeError: window.require is not a function

我知道这是打字稿。要求。不是解决办法。但进口也不起作用。我还试着构建一个普通的javascript应用程序来测试它,但这也不起作用。

我还确信,即使我得到了正确的导入,我也应该得到一个constructor error。有没有人在这个问题上有经验,能不能请你帮我。

电子main.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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"
  ]
}
EN

回答 2

Stack Overflow用户

发布于 2022-07-11 04:31:17

在main.ts中:

代码语言:javascript
复制
 webPreferences: {
   nodeIntegration: true,
   contextIsolation: false,
   enableRemoteModule: true,
   preload: path.join(__dirname, 'preload.js'),
},
票数 0
EN

Stack Overflow用户

发布于 2022-08-29 21:11:32

我正在使用它,它工作正常:

它在main.ts中使用require,但在React代码中使用import

它使用import意味着所需的模块由Webpack在构建时包含在呈现程序包中,而不是在运行时从of Node.js环境加载。

它根本不使用window.require (因此,据我所知,没有问题)。

或者有这个(我没有用):

它谈到了如何使用window.require

我们需要使用ipcRenderer导入window.require模块,因为我们希望在运行时需要从节点环境中获取电子,而不是webpack在编译过程中使用的电子。链接到github发布的这里

我不知道为什么它想要求在运行时而不是在编译期间。

它链接到这个问题,这个问题已经结束,并有93条评论:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72160269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档