问题
I am building a app with React and Electron and am using React Router Dom for navigation (I am using the HashRouter as it will not be on a server in production). I am trying to open a new electron window and display a page made with React in it but I cannot figure out how to access a react router route using the file protocol because each page does not get its own html file. Does anyone know how I can access that route using the file protocol?
My home page is loaded into the app using this which works for the "/" route:
mainWin.loadURL(
isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, '../build/index.html')}`
)
I am trying to load a page into a second window which should use the "/settings" route.
settingsWin.loadURL(
isDev
? 'http://localhost:3000/settings'
: `file://${path.join(__dirname, '../build/index.html/settings')}`
)
Here are the paths that I have tried to use but none of them load the page:
`file://${path.join(__dirname, '../build/index.html:settings')}`
`file://${path.join(__dirname, '../build/settings')}`
`file://${path.join(__dirname, '../build/index.html/settings')}`
回答1:
The way to access a React Router route locally would be to add "#/[page-name]" at the end of the index.html path because the HashRouter looks at the URL hash (URL fragment). So now my code looks like this:
`file://${path.join(__dirname, '../build/index.html#/settings')}`
来源:https://stackoverflow.com/questions/58583679/using-file-protocol-to-access-react-router-route-locally