在react-router-dom中获得404错误

时间:2017-07-23 09:24:55

标签: reactjs react-redux react-router-v4

我已经开始使用react-router-dom了。我的主页面成功呈现,但是当我点击Link组件提供的超级链接时,它不会重定向但是如果我手动点击url则会给我404错误。请帮帮我吗?

这是我的配置文件

import React from 'react'
import { render } from 'react-dom'
import { createStore, applyMiddleware,compose } from 'redux'
import { Provider } from 'react-redux'
//import rootReducer from './reducers'
import createLogger from 'redux-logger'
import thunk from 'redux-thunk'
import {BrowserRouter} from 'react-router-dom'

import promise from "redux-promise-middleware"
import logger from "redux-logger"
import {fetchUsers} from "./action/UserAction"
import {fetchChart} from "./action/ChartAction"
import {fetchNames} from "./action/SearchAction"
import reducer from "./reducers"
import routes from "./routes"

const middleware = applyMiddleware(promise(), thunk, logger())
const store= createStore(reducer,middleware)

store.dispatch(fetchUsers());
store.dispatch(fetchChart());
render(
  <Provider store={store}>
    <BrowserRouter >
      {routes}
    </BrowserRouter>

  </Provider>,
  document.getElementById('root')
)

我的路线档案

import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route } from "react-router-dom"
export default (
  <switch>
  <Route path="/" component={App}/>
  <Route path="/users" component={Users}/>
    <Route path="/charts" name="charts" component={Charts}/>
 </switch>

);

App.js

import {Link} from "react-router-dom"
import React from "react"
const App = () =>(

<div>
<h1> this is main page </h1>
<Link to="charts">charts</Link>
</div>
)

export default App

Users.js

import React from "react"
const User = () =>(
    <div>
    <h1> this is user </h1>
    </div>
)

export default User

Charts.js

import React from "react"
const Chart = () => (
    <h1> this is chart </h1>
)

export default Chart

更新1 : 我不知道发生了什么事。但我改变了routes.js中的序列,它通过点击url来工作。但是如果我手动点击网址或刷新它仍然无法正常工作。下面是我更新的routes.js

import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route,Switch } from "react-router-dom"

export default (
  <Switch>
    <Route  path="/charts"  component={Charts}/>
    <Route  path="/users" component={Users}/>
  <Route path="/" component={App}/>


 </Switch>

);

更新2: 如果我使用精确路径,则序列无关紧要。我想我并不能正确理解精确路径的使用。但仍然手动击打网址或刷新对我不起作用  我最后的routes.js

import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route,Switch } from "react-router-dom"

export default (
  <Switch>


  <Route exact path="/" component={App}/>
  <Route  exact path="/charts"  component={Charts}/>
  <Route exact path="/users" component={Users}/>

 </Switch>

);

2 个答案:

答案 0 :(得分:2)

在挣扎之后,我得到一篇SO帖子说如果我的数据不是从后端映射,我应该使用HashRouter。所以我改变了我的浏览器,所以我将代码更改为此。但我不知道为什么浏览器路由器没有工作

<Provider store={store}>
    <HashRouter >
      {routes}
    </HashRouter>

  </Provider>,
  document.getElementById('root')

答案 1 :(得分:0)

如果您使用的是webpack,请添加此

devServer: {
    historyApiFallback: true,
}

使用BrowserRouter时“ historyApiFallback ”应该是真的