页面路由

为了使用路由,先下好react-router-dom

  1. cnpm install react-router-dom --save

该blog项目总共有四个页面

  • Home:页面主页
  • Display:阅读文章的页面
  • Edit:编辑文章的页面
  • Login:登录页面

为了演示路由,在pages中分别新建Home, Display, Edit, Login四个文件夹,并在每个文件夹中新建index.jsx,同时在Edit文件夹了新建components文件夹,将RichText文件夹全部移入到这个文件夹中(因为RichText富文本编辑器属于编辑文章页面的一部分),简单的在每个文件的index.jsx写下一些内容,具体的内容在后面添加。

  1. // Home/index.jsx
  2. import React from 'react'
  3. import BasicLayout from './../../layouts/BasicLayout'
  4. import {withRouter} from 'react-router-dom'
  5. function Home(props) {
  6. return (
  7. <BasicLayout>
  8. Home
  9. </BasicLayout>
  10. )
  11. }
  12. export default withRouter(Home)
  1. // Display/index.jsx
  2. import React from 'react'
  3. import BasicLayout from './../../layouts/BasicLayout'
  4. import {withRouter} from 'react-router-dom'
  5. function Display(props) {
  6. return (
  7. <BasicLayout>
  8. Display
  9. </BasicLayout>
  10. )
  11. }
  12. export default withRouter(Display)
  1. // Edit/index.jsx
  2. import React from 'react'
  3. import BasicLayout from './../../layouts/BasicLayout'
  4. import {withRouter} from 'react-router-dom'
  5. import RichText from './components/RichText'
  6. function Edit() {
  7. return (
  8. <BasicLayout>
  9. <RichText />
  10. </BasicLayout>
  11. )
  12. }
  13. export default withRouter(Edit)
  1. // Login/index.jsx
  2. import React from 'react'
  3. import {withRouter} from 'react-router-dom'
  4. import LoginLayout from './../../layouts/LoginLayout'
  5. function Login(props) {
  6. return (
  7. <LoginLayout>
  8. login
  9. </LoginLayout>
  10. )
  11. }
  12. export default withRouter(Login)

设计页面路由如下

path component redirect
/home Home
/display/:id Display
/edit Edit
/edit:id Edit
/login Login
/ /home

注意到Edit组件对应两个路径,因为编辑文章有两种情况,第一种是添加文章,这时是/edit路径,第二种是编辑文章,这时需要传入文章的id,所以这时是/edit/:id路径。

在src下新建文件夹config,在config新建routes.js,里面设置路由信息,如下

  1. import Home from './../pages/Home/index'
  2. import Display from './../pages/Display/index'
  3. import Edit from './../pages/Edit/index'
  4. import Login from './../pages/Login/index'
  5. export default [
  6. {
  7. path: "/login",
  8. component: Login
  9. },
  10. {
  11. path: "/home",
  12. component: Home
  13. },
  14. {
  15. path: "/display/:id",
  16. component: Display
  17. },
  18. {
  19. path: "/edit",
  20. component: Edit
  21. },
  22. {
  23. path: "/edit/:id",
  24. component: Edit
  25. },
  26. {
  27. path: "/",
  28. redirect: "/home",
  29. exact: true
  30. },
  31. ]

在src下新建router.js,用来渲染路由,内容如下

  1. import React from 'react'
  2. import routes from './config/routes'
  3. import {Switch, BrowserRouter as Router, Route, Redirect } from 'react-router-dom'
  4. import { createBrowserHistory } from 'history';
  5. const history = createBrowserHistory();
  6. // 返回可能是Redirect 也可能是Route 所以使用RouteItem封装
  7. const RouteItem = (props) => {
  8. const { path, component: Component, redirect, key, exact } = props;
  9. if (redirect) {
  10. return <Redirect from={path} to={redirect} key={key} />
  11. }
  12. return (
  13. <Route
  14. key={key}
  15. exact={exact}
  16. path={path}
  17. render={componentProps => {
  18. return (
  19. <Component {...componentProps} />
  20. )
  21. }}
  22. />
  23. );
  24. };
  25. const router = () => {
  26. return (
  27. <Router>
  28. {/* Switch 唯一匹配 */}
  29. <Switch>
  30. {routes.map((item, id) => {
  31. return RouteItem({ key: id, ...item })
  32. })}
  33. </Switch>
  34. </Router>
  35. );
  36. };
  37. export default router;

接着在src/index.js中渲染出来

  1. import ReactDOM from 'react-dom';
  2. import router from './router'
  3. import './common.css';
  4. ReactDOM.render(router(), document.getElementById("root"));

接着启动项目(npm start或yarn start),改变浏览的url(如localhost:3000/display)看看页面是否能成功跳转(我这里是没有问题的,如果你不能的话,回过头仔细看看吧)。