首页 > 解决方案 > Electron - React - React Router 加载自定义路由(例如:example.com/#/popup)

问题描述

我在搞乱反应,反应路由器和电子,我创建了一个像这样的简单路由器:

export const Router: FC = () => {
    return (
        <HashRouter>
            <Switch>
                <Route exact path="/">
                    <IndexPage />
                </Route>
                <Route path="/stats">
                    <StatsPage />
                </Route>
                <Route path="/popup">
                    <PopupPage />
                </Route>
            </Switch>
        </HashRouter>
    );
};

我可以在浏览器和电子应用程序上看到所有路线,没有问题,但有问题。如何/popup直接加载页面<ElectronBrowserWindow>.loadURL();?这是我所拥有的:

    popupWindow = new BrowserWindow({
        width: 260,
        height: 360,
        x: 0,
        y: 0,
        resizable: false,
        alwaysOnTop: true,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true,
            devTools: false,
            contextIsolation: false,
        },
        frame: false,
        icon,
        title: "Brawlhalla Stats",
    });
    popupWindow.loadURL(
        isDev
            ? "http://localhost:3000/#/popup" // this one works
            : `file://${path.join(__dirname, "../build/index.html?#/popup")}`, // this one not
    );

感谢您的所有帮助:3

标签: reactjsreact-routerelectronreact-router-domelectron-builder

解决方案


好吧,我只是环顾四周,发现了同样的帖子:Get Electron production window to load a route with loadURL?

这是@IronWaffleMan 的回答:https ://stackoverflow.com/a/65068333/10124281

基本上,我只需要输入file://${path.join(__dirname, "../build/index.html#popup")}


推荐阅读