首页 > 解决方案 > Electron - Webview 必须附加到 DOM 错误

问题描述

在我的 Electron/React 应用程序中,我可以打开托管在另一个 Web 应用程序上的视频内容。我所做的是webview使用 URL 呈现所需页面。这是我的例子:

const App = ({ computedMatch }) => {
  const { audioMuted } = useSelector(({ window }) => window);
  const titleId = computedMatch.params.id;
  const src = `${BASE_URL}/${titleId}`;

  useEffect(() => {
    const webview = document.querySelector('webview');
    if (webview) {
      webview.setAudioMuted(audioMuted);
    }
  }, [audioMuted]);

  return <webview src={src} />;
};

在 Windows 栏上,我有一个静音按钮图标,按下静音按钮,将 Redux 状态更新audioMutedtrue。当状态发生变化时,useEffect就会发生作用

一方面,这个解决方案似乎有效,但经过一些测试,有时我会收到这个错误:

error: uncaughtException: The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.
Error: The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.

如何修复此错误以使其永远不会出现?

标签: reactjswebviewelectron

解决方案


推荐阅读