[英]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.