官方文档:https://reactrouter.com/web/guides/quick-start

安装

安装react-router

  1. yarn add react-router-dom

安装@types/react-router-dom

  1. yarn add --dev @types/react-router-dom

使用示例

  1. import React from 'react';
  2. import {
  3. BrowserRouter as Router,
  4. Switch,
  5. Route,
  6. Link,
  7. Redirect
  8. } from "react-router-dom";
  9. export default function App() {
  10. return (
  11. <Router>
  12. <div>
  13. <nav>
  14. <ul>
  15. <li>
  16. <Link to="/tags">标签</Link>
  17. </li>
  18. <li>
  19. <Link to="/money">记账</Link>
  20. </li>
  21. <li>
  22. <Link to="/statistics">统计</Link>
  23. </li>
  24. </ul>
  25. </nav>
  26. <Switch>
  27. <Route path="/tags">
  28. <Tags />
  29. </Route>
  30. <Route path="/money">
  31. <Money />
  32. </Route>
  33. <Route path="/statistics">
  34. <Statistics />
  35. </Route>
  36. <Route exact path="/">
  37. <Redirect to="/money" />
  38. </Route>
  39. <Route path="*">
  40. <NotFound />
  41. </Route>
  42. </Switch>
  43. </div>
  44. </Router>
  45. );
  46. }
  47. function NotFound(){
  48. return <h2>你访问的页面不存在</h2>
  49. }
  50. function Statistics() {
  51. return <h2>统计页</h2>;
  52. }
  53. function Tags() {
  54. return <h2>标签页</h2>;
  55. }
  56. function Money() {
  57. return <h2>记账页</h2>;
  58. }

注意重定向的写法
添加exact则不模糊匹配

  1. <Route exact path="/">
  2. <Redirect to="/money" />
  3. </Route>

还可以写成

  1. <Redirect exact from="/" to="/money" />

其他路径跳转404页面

  1. <Route path="*">
  2. <NotFound />
  3. </Route>

history v.s. hash

Router有2种模式: History 和Hash
如果没有后台服务器,则只能用Hash,hash模式的url都带#号
如果有后台服务器,需要后端配置所有路径都重定向到首页才能用History

image.png
image.png
修改Hash模式:将BrowserRouter改为HashRouter

  1. import {
  2. // BrowserRouter as Router,
  3. HashRouter as Router,
  4. Switch,
  5. Route,
  6. Link,
  7. Redirect
  8. } from "react-router-dom";

index.scss

新建index.scss文件,在里面添加reset

  1. @import-normalize;
  2. * {margin:0;padding:0;}
  3. * {box-sizing: border-box;}
  4. *::after, *::before {box-sizing: border-box;}
  5. ul, ol {
  6. list-style: none;
  7. }
  8. a {
  9. text-decoration: none; color: inherit;
  10. }

在index.tsx中引入

  1. import './index.scss';

styled-component

修改导航栏布局和基本样式

  1. import styled from 'styled-components';
  2. const Wrapper = styled.div`
  3. height: 100vh;
  4. display: flex;
  5. flex-direction: column;
  6. `
  7. const Main = styled.div`
  8. flex-grow: 1;
  9. overflow: auto;
  10. `
  11. const Nav = styled.nav`
  12. > ul {
  13. display: flex;
  14. > li {
  15. width: 33.3333%;
  16. text-align: center;
  17. padding: 16px;
  18. }
  19. }
  20. `
  21. function App() {
  22. return (
  23. <Router>
  24. <Wrapper>
  25. <Main>
  26. <Switch>
  27. <Route path="/tags">
  28. <Tags />
  29. </Route>
  30. // ...
  31. </Switch>
  32. </Main>
  33. <Nav>
  34. <ul>
  35. <li>
  36. <Link to="/tags">标签</Link>
  37. </li>
  38. // ...
  39. </ul>
  40. </Nav>
  41. </Wrapper>
  42. </Router>
  43. );
  44. }

中文字体

搜索: 中文字体 css github
image.png
网页一般用黑体,在helper.scss中声明字体变量$font-hei, 给body添加font-family为$font-hei

将Nav组件移动到components目录

  1. import styled from 'styled-components';
  2. import {Link} from 'react-router-dom';
  3. import React from 'react';
  4. const NavWrapper = styled.nav`
  5. line-height: 24px;
  6. box-shadow: 0 0 3px rgba(0,0,0,0.2);
  7. > ul {
  8. display: flex;
  9. > li {
  10. width: 33.3333%;
  11. text-align: center;
  12. padding: 16px;
  13. }
  14. }
  15. `
  16. const Nav = () => {
  17. return (
  18. <NavWrapper>
  19. <ul>
  20. <li>
  21. <Link to="/tags">标签</Link>
  22. </li>
  23. <li>
  24. <Link to="/money">记账</Link>
  25. </li>
  26. <li>
  27. <Link to="/statistics">统计</Link>
  28. </li>
  29. </ul>
  30. </NavWrapper>
  31. )
  32. }
  33. export default Nav;