gpt4 book ai didi

reactjs - preload.ts中的contextBridge.exposeInMainWorld : ipcRenderer receives the message from main. ts,但渲染器无法获取它

转载 作者:行者123 更新时间:2023-12-03 12:38:11 24 4
gpt4 key购买 nike

在一个Electron-React-Typescript应用程序中,我设法在预加载中通过contextBridge.exposeInMainWorld将消息从渲染器进程发送到main,但是在返回的过程中,来自main的处理后的输出实际上通过了thrugh并到达了preload.ts,但是它没有无法到达渲染器进程。
这一定是真正的“愚蠢”,但我不知道什么是“罪魁祸首”。所以,请问您的帮助。
preload.ts:

const {
contextBridge,
ipcRenderer
} = require("electron")


contextBridge.exposeInMainWorld(
"api", {
send: (channel, data) => {
ipcRenderer.invoke(channel, data).catch(e => console.log(e))
},
receive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(...args))
//ipcRenderer.on(channel, (event, ...args) => {
//console.log("ipcRenderer.on-args: ", args);
//});
}
)
index.d.ts:
declare interface Window {
api: {
send: (channel: string, ...arg: any) => void;
receive: (channel: string, func: (event: any, ...arg: any) => void) => void;
}
}
main.ts:
ipcMain.handle("path-to-url", (IpcMainEvent, path) => {
console.log("received a request from viewerDemo");
if (mainWindow) {
let urlified = url.pathToFileURL(path);
console.log("urlified = url.pathToFileURL(path) : ", urlified);
console.log("urlified.href= ", urlified.href);
console.log("urlified.pathname= ", url.pathToFileURL(path).pathname);
mainWindow.webContents.on("dom-ready", () => {
mainWindow.webContents.send("path-to-url", "ohhhhhhhhhhhhhhhh");
mainWindow.webContents.send("path-to-url", url.pathToFileURL(path).pathname);
});
}
});
渲染器进程(数据无法正确显示!):
viewerDemo.tsx
import * as React from 'react';
import Viewer from 'react-viewer';

let path_1 = './images/natural-scene.jpeg';
let url_path;

const pathToUrlFunc = (path): string => {
console.log("pathToUrlFunc called");
window.api.send("path-to-url", path);
let urlPath;
window.api.receive("path-to-url", (event, args) => {
console.log("window.api.receive called!");
console.log("window.api.receive-args[0]: ", args[0]);
urlPath = args[0];
});
return urlPath;
}

function ViewerDemo() {
const [ visible, setVisible ] = React.useState(false);

let urlpath = pathToUrlFunc(path_1);
console.log("urlpath: ", urlpath);

return (
<div>
<button onClick={() => { setVisible(true); } }>show</button>
<Viewer
visible={visible}
onClose={() => { setVisible(false); } }
images={[{src: 'https://www.fotolip.com/wp-content/uploads/2016/05/Nature-Scenes-
21.jpg', alt: ''}]}
//images={[{src: urlpath, alt: ''}]}

/>
</div>
);
}

export default ViewerDemo;
在主要我得到: path received from main: ./images/natural-scene.jpeg而从主返回渲染器的方式是: Cannot read property '0' of undefined enter image description here
如果在api的“接收”部分的contextBridge.exposeInMainWorld中,我将放置:
  receive: (channel, func) => {
//ipcRenderer.on(channel, (event, ...args) => func(...args))
ipcRenderer.on(channel, (event, ...args) => {
console.log("ipcRenderer.on-args: ", args);
});
}
我再也没有收到该错误,在preload.ts中出现了来自main.ts的正确消息,但渲染器进程仍未得到它:
enter image description here

最佳答案

您可能现在已经找到了答案,但这只是为了以防万一,并且可以使遇到此问题的任何人受益。尽管我无法运行代码来证明这是问题所在,但我认为这是预加载接收功能的问题:
ipcRenderer.on代理方法仅使用args参数的传播调用“func”处理程序。但是在viewerDemo.tsx中,传递给“receive”的处理程序方法被称为“func”,它期望两个参数“event”和“args”。因此,您在args中传递的单个参数可能会在“事件”参数中显示为单个元素数组。
为了检验该理论并希望解决此问题,请使方法一致。您可以在以下代码中的viewerDemo.tsx中的处理程序中更改此行:window.api.receive("path-to-url", (event, args) => {window.api.receive("path-to-url", (args) => {或者在preload.ts中更改这一行ipcRenderer.on(channel, (event, ...args) => func(...args))ipcRenderer.on(channel, (event, ...args) => func(event, ...args))两者都应解决该问题。

关于reactjs - preload.ts中的contextBridge.exposeInMainWorld : ipcRenderer receives the message from main. ts,但渲染器无法获取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65408809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com