如果用户导航到其他路线,Electron reactjs应用程序会在重新加载后显示空白页面

时间:2019-07-22 09:18:36

标签: javascript node.js reactjs electron electron-builder

我用create-react-app创建我的项目。我将react-router-dom用于路由器,并将electron-builder用于打包我的应用程序。

当我第一次运行我的应用程序时,它可以正常工作,但是一旦我重新加载页面,所有页面都会变成白色。如果我没有导航到其他路线,则该应用程序在重新加载后仍然可以运行。

下面是我的电子/index.js代码

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 1280,
    height: 600,
    minWidth: 800,
    minHeight: 200,
    autoHideMenuBar: true,
    useContentSize: true,
    resizable: true,
    webPreferences: {
      nodeIntegration: true,
    },
  });
  mainWindow.loadURL(isDev
    ? 'http://localhost:3000'
    : `file://${path.join(__dirname, '../build/index.html')}`);
  mainWindow.focus();

  mainWindow.on('close', (e) => {
    if (willQuitApp) {
      /* the user tried to quit the app */
      mainWindow = null;
    } else {
      /* the user only tried to close the window */
      e.preventDefault();
      mainWindow.hide();
    }
  });
}

app.on('ready', createWindow);

2 个答案:

答案 0 :(得分:1)

我找到了答案。我只需要使用<HashRouter>而不是<BrowserRouter>

答案 1 :(得分:0)

重新加载后,您得到了一个空白页面,因为导航到另一个页面时,您会更改URL。因此,当您重新加载时,电子将加载该网址,而不是index.html文件