簡體   English   中英

有條件地將電子導入 react-native-web 應用程序

[英]Importing electron conditionally into react-native-web app

我目前有一個針對 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');

但是我仍然收到同樣的錯誤。

謝謝 :)

找到了解決我的問題的方法:)

我做了2個文件

ipcRenderer.js包含一個空對象的導出

ipcRenderer.web.js ,其中包含ipcRenderer.web.js isElectron()檢查並從電子導出 null 或ipcRenderer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM