1. import Nav from './Nav';
  2. import React from 'react';
  3. import styled from 'styled-components';
  4. const Wrapper = styled.div`
  5. height: 100vh;
  6. display: flex;
  7. flex-direction: column;
  8. `
  9. const Main = styled.div`
  10. flex-grow: 1;
  11. overflow: auto;
  12. `
  13. interface Props {
  14. children: React.ReactNode
  15. }
  16. const Layout = (props: Props) => {
  17. return (
  18. <Wrapper>
  19. <Main>
  20. {props.children}
  21. </Main>
  22. <Nav />
  23. </Wrapper>
  24. )
  25. }
  26. export default Layout
  • props.children的类型声明为React.ReactNode

将其他所有子组件都封装到views目录
image.png

  1. import Layout from '../components/Layout';
  2. import React from 'react';
  3. function Money() {
  4. return (
  5. <Layout>
  6. <h2>记账页</h2>
  7. </Layout>
  8. );
  9. }
  10. export default Money;

修复导航栏点击不到图标的问题

将Icon移到Link标签里面

  1. <ul>
  2. <li>
  3. <Link to="/tags">
  4. <Icon name="label" />
  5. 标签
  6. </Link>
  7. </li>
  8. <li>
  9. <Link to="/money">
  10. <Icon name="money" />
  11. 记账
  12. </Link>
  13. </li>
  14. <li>
  15. <Link to="/statistics">
  16. <Icon name="statistics" />
  17. 统计
  18. </Link>
  19. </li>
  20. </ul>

Link元素在页面中实际上是a标签
image.png

React-Router的activeStyle

实现点击图标时高亮,使用NavLink的activeStyle或activeClassName属性 参考文档
将Link改为NavLink

  1. <ul>
  2. <li>
  3. <NavLink to="/tags" activeClassName="selected">
  4. <Icon name="label"/>
  5. 标签
  6. </NavLink>
  7. </li>
  8. // ...
  9. </ul>

设置css样式

  1. a.selected {
  2. color: red;
  3. .icon {
  4. fill: red;
  5. }

使用svgo-loader去除svg的fill属性

修改webpack中的配置如下

  1. {
  2. test: /\.svg$/,
  3. use: [
  4. {loader: 'svg-sprite-loader', options: {}},
  5. {
  6. loader: 'svgo-loader', options: {
  7. plugins: [
  8. {
  9. name: 'removeAttrs',
  10. params: {
  11. attrs: 'fill'
  12. }
  13. }
  14. ]
  15. }
  16. },
  17. ]
  18. },