【问题标题】:Importing electron conditionally into react-native-web app有条件地将电子导入 react-native-web 应用程序
【发布时间】:2020-06-21 21:43:41
【问题描述】:

我目前有一个针对 Android 和 Electron 的 react-native-web 应用程序。

我在应用程序中添加了电子快捷方式,并在 react 组件中使用 ipcRenderer 来监听加速器并运行特定于 react 的代码。

问题是,除非我将ipcRenderer 导入.web.js 文件,否则在运行Android 时会收到以下错误:

Unable to resolve module `fs` from `/Users/.../node_modules/electron/index.js`: Module `fs` does not exist in the Haste module map

导致问题的代码片段:

if (isElectron()) {
    const { ipcRenderer } = require('electron'); <-- this line

我尝试将导入包装在 Platform.OS 条件中,就像这样

if (Platform.OS === 'web') {
    const isElectron = require('is-electron');

    if (isElectron()) {
        const { ipcRenderer } = require('electron');

但是我仍然收到同样的错误。

谢谢你:)

【问题讨论】:

    标签: javascript react-native electron electron-builder react-native-web


    【解决方案1】:

    找到解决我问题的方法:)

    我做了2个文件

    ipcRenderer.js 包含一个空对象的导出

    ipcRenderer.web.js 包含 isElectron() 检查并从电子导出 null 或 ipcRenderer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-20
      • 2016-06-21
      • 2018-09-09
      • 2012-08-05
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多