react实现路由可以直接使用react-router。 ReactRouter是由Ryan Florence开发的应用于ReactJS的路由组件,它通过定义ReactJS组件<Routes>及相关子组件来实现页面路由的映射、参数的解析和传递。 以下是例子: var ReactRouter = require('react-router'); var Routes = ReactRouter.Routes; var Route = ReactRouter.Route;
//定义整个页面的路由结构 var routes = ( <Routes location="hash"> <Route path="/" handler={App}> <Route path="books" name="bookList" handler={Books}/> <Route path="movies" name="movieList" handler={Movies}/> </Route> </Routes> ); 与之前版本不同,在 react-router4 中,渲染<redirect>组件会直接跳转到目标位置。from 属性仅用于<redirect>包裹着<Switch> 组件中的时候。 Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do <Redirect to="/somewhere/else"/> // render 该组件发生跳转
From: A pathname to redirect from. This can only be used to match a location when rendering a <Redirect> inside of a <Switch>. See <Switch children> for more details.