使用文件协议在本地访问react-router路由?

IT技术 reactjs react-router electron
2021-05-11 12:18:31

我正在使用 React 和 Electron 构建一个应用程序,并使用 React Router Dom 进行导航(我使用的是 HashRouter,因为它不会在生产中的服务器上)。我正在尝试打开一个新的电子窗口并在其中显示一个用 React 制作的页面,但我无法弄清楚如何使用文件协议访问 React 路由器路由,因为每个页面都没有自己的 html 文件。有谁知道我如何使用文件协议访问该路由?

我的主页使用这个适用于“/”路线加载到应用程序中:

mainWin.loadURL(
    isDev
        ? 'http://localhost:3000'
        : `file://${path.join(__dirname, '../build/index.html')}`
)

我正在尝试将页面加载到应该使用“/settings”路由的第二个窗口中。

settingsWin.loadURL(
    isDev
        ? 'http://localhost:3000/settings'
        : `file://${path.join(__dirname, '../build/index.html/settings')}`
)

以下是我尝试使用但都没有加载页面的路径:

`file://${path.join(__dirname, '../build/index.html:settings')}`
`file://${path.join(__dirname, '../build/settings')}`
`file://${path.join(__dirname, '../build/index.html/settings')}`
1个回答

在本地访问 React Router 路由的方法是在 index.html 路径的末尾添加“#/[page-name]”,因为 HashRouter 查看 URL 哈希(URL 片段)。所以现在我的代码如下所示:

`file://${path.join(__dirname, '../build/index.html#/settings')}`