Electron React 应用程序路由在生产中返回空白页面

发布于 2025-01-11 05:08:56 字数 2486 浏览 0 评论 0原文

嘿,我终于完成了我的反应电子应用程序,它在开发中运行良好,但是,当我单击主屏幕上的按钮将我引导到不同的页面时,它会返回空白页面。

尝试的解决方案

  • 我确实使用 HashRouter 而不是普通的 Router
  • 尝试在 file://${path.join(__dirname, "../build/index.html# ")}

问题 基本上,路线“/”处的主菜单显示,backgroundProcess窗口也显示,但是当我单击按钮将我定向到/慢或/快等时,它会返回一个空白页面

我的窗口在main.js文件中:

mainWindow.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/index.html#")}`);
backgroundProcess.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/woker.html")}`)
configWindow.loadURL(isDev ? `http://localhost:3000#/config${mode.toString()}` : `file://${path.join(__dirname, `../build/index.html#config${mode}`)}`);
AbiWindow.loadURL(isDev ? "http://localhost:3000#/abi" : `file://${path.join(__dirname, "../build/index.html#abi")}`);

App.js路由:

  <HashRouter>
      <Route exact path='/' component={Mainmenu}/>
      <Route exact path='/fast' component={Fast}/>
      <Route exact path='/config0' component={ConfigWindow}/>
      <Route exact path='/abi' component={AbiWindow}/>
      <Route exact path='/slow' component={Slow}/>
      <Route exact path='/config1' component={SlowConfig}/>
      <Route exact path='/sell' component={Sell}/>
      <Route exact path='/config2' component={SellConfig}/>
  </HashRouter>

MainMenu 按钮路由:

function MainMenu() {
    return ( 
        <div className='no-gutters'>
            <div className='col no-gutters'>
                <div className='fast d-flex justify-content-center align-items-center'>
                    <Link className='fastbtn' to="/fast">FAST</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='slow d-flex justify-content-center align-items-center'>
                    <Link className='slowbtn' to="/slow">SLOW</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='sell d-flex justify-content-center align-items-center'>
                    <Link className='sellbtn' to="/sell">SELL</Link>
                </div>
            </div>
        </div>
     );
}


非常感谢任何帮助 提前致谢 :)

Hey so i finally finished my react electron app and it works well in development however, it returns blank pages when i click on a button on the homescreen to direct me to a different page.

Attempted solutions

  • I do use HashRouter instead of normal Router
  • Tried adding a # at the end of file://${path.join(__dirname, "../build/index.html#")}

Problem
Basically the Main Menu at route "/" shows up and so does the backgroundProcess window but when i click on a button to direct me to /slow or /fast etc it returns a blank page

My windows in main.js file:

mainWindow.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/index.html#")}`);
backgroundProcess.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/woker.html")}`)
configWindow.loadURL(isDev ? `http://localhost:3000#/config${mode.toString()}` : `file://${path.join(__dirname, `../build/index.html#config${mode}`)}`);
AbiWindow.loadURL(isDev ? "http://localhost:3000#/abi" : `file://${path.join(__dirname, "../build/index.html#abi")}`);

App.js routing:

  <HashRouter>
      <Route exact path='/' component={Mainmenu}/>
      <Route exact path='/fast' component={Fast}/>
      <Route exact path='/config0' component={ConfigWindow}/>
      <Route exact path='/abi' component={AbiWindow}/>
      <Route exact path='/slow' component={Slow}/>
      <Route exact path='/config1' component={SlowConfig}/>
      <Route exact path='/sell' component={Sell}/>
      <Route exact path='/config2' component={SellConfig}/>
  </HashRouter>

MainMenu Button routing:

function MainMenu() {
    return ( 
        <div className='no-gutters'>
            <div className='col no-gutters'>
                <div className='fast d-flex justify-content-center align-items-center'>
                    <Link className='fastbtn' to="/fast">FAST</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='slow d-flex justify-content-center align-items-center'>
                    <Link className='slowbtn' to="/slow">SLOW</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='sell d-flex justify-content-center align-items-center'>
                    <Link className='sellbtn' to="/sell">SELL</Link>
                </div>
            </div>
        </div>
     );
}


Any help is greatly appreciated
Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2025-01-18 05:08:56

如果您使用的是最新的 Electron,您可以将 更改为 。至少在我的测试中,两个路由器都应该在 Electron 中正常工作。正是 basename 导致了空白屏幕。

这应该让渲染进程知道正确的 url 在哪里。

另外,package.json 中的 homepage 仍应为 ./

这就是我今天所做的以及相应的发布。查看 src/App.tsx 的更改

https://github.com/wongchito/RailMapGenerator/commit/1a2a5dbf3c5dca710f168837649e2b77e347c6b0

https://github.com/wongchito/RailMapGenerator/releases/tag/3.12.2

If you are using the lastest electron, you may change <HashRouter> to <BrowserRouter basename={'/'}>. Both router should work fine in Electron, at least in my test. It is the basename that results a blank screen.

This should let the render process know where the right url is.

Also the homepage in the package.json should still be ./.

This is what I did today and the corresponding release. See the change of src/App.tsx

https://github.com/wongchito/RailMapGenerator/commit/1a2a5dbf3c5dca710f168837649e2b77e347c6b0

https://github.com/wongchito/RailMapGenerator/releases/tag/3.12.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文