我现在发现React Router,我有一个问题,为什么我可以用hashHistory直接在地址栏中输入URL,而不能用browserHistory?
这里是我尝试访问的网址:
localhost:3000/element/createform
这是我的代码:
// import {...}
// then the code content
<ApolloProvider client={client} >
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={ElementList}/>
<Route path="element/createform" component={CreateForm} exact/>
</Route>
</Router>
</ApolloProvider>
对于browserHistory,它会返回我"无法获取路径",但对于hashHistory,这是有效的。那为什么呢?
您需要web服务器始终返回主HTML文件。
如果你调用example.com/index.html/test,你的浏览器会请求example.com/index.html,然后让index.html处理散列(至少在这里(。
如果您调用example.com/index.html/test,您的浏览器将请求另一个名为test的文件,该文件在服务器上找不到。