简体   繁体   中英

HashHistory vs. BrowserHistory

I am using Node with Webpack-Dev-Server and React-Router.

In my app.js file I have:

<Router history={browserHistory}>
  <Route path="/" component={Layout}>
    <IndexRoute component={FindFriends} />
    <Route path="/you" component={YouAndYourFriends} />
  </Route>
</Router>

Using browserHistory the browser returns an error: Cannot GET /you

When I change browserHistory to hashHistory , everything works fine.

Where is my mistake? Or what should you do using browserHistory ?

I had this problem. Solved it by adding the following to my webpack.config.js

devServer: {
  historyApiFallback: true,
  contentBase: path.join(__dirname, "dist"), // Not Related but important
}

You can also toggle this option via the command-line: https://webpack.github.io/docs/webpack-dev-server.html#the-historyapifallback-option

In my case, I got it working by putting it in the config file.

Good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM