Umi、React框架学习笔记

本文介绍了Umi脚手架如何实现CssModules进行样式隔离,通过Less的className生成唯一Hash,以及组件中引用样式的方法。同时,讲解了Umi的路由配置,包括基本配置、子路由设置和路由对象的使用。此外,还提及了页面的keep-alive功能,通过umi-plugin-keep-alive插件实现页面缓存控制。
摘要由CSDN通过智能技术生成

一、Css样式隔离

  • Umi脚手架自带Less+Css Modules 实现样式隔离。
  • 原理:基于less中的className生成唯一Hash串。
  • 类似Scoped,一个基于标签(v-xx),一个基于className。
  • 推荐写法
// less
.wrap-box {
  background: pink;
  .cont-text-box{
    border:1px solid #ccc;
    padding: 4px;
    .cont-text{
      font-size: 16px;
      color: #2b2b2b;
    }
  }
}

// Component
import styles from './index.less';
export default function TestCompoent() {
  return (
    <div className={styles['wrap-box']}>
      <div className={styles['cont-text-box']}>
        <div className={styles['cont-text']}>这是一段描述文字!</div>
      </div>
    </div>
  );
}

原理

{
 "wrap-box":"wrap-box___3XsTJ",
 "cont-text-box":"cont-text-box___2ScBT",
 "cont-text":"cont-text___3comu
}

二、路由配置

  • 官方路由配置文档
  • 路由配置文件 => /.umirc.ts,可以抽离为单独配置文件。
  • 类似VueRouter配置方式,避免频繁操作ReactRouterDom。
// 配置案例
[
  { exact: true, path: '/index', component: './Index/index' },
  {
    path: '/user',
    component: './User/index',
    routes: [{ path: '/user/detail', component: './UserDetail/index' }],
  },
  { exact: true, path: '/', redirect: '/index' },
]
  • component参数为字符串,Umi会做自动导入。
    • exact 严格匹配
    • routes 子路由
    • redirect 重定向
  • wrappers 可以结合hooks实现路由拦截。

三、子路由配置

  • 子路配置与VueRouter相同,通过将子路由组件注入到当前组件的props.children实现。
  • 官方推荐使用layout组件控制路由渲染,也可以采用类似router-view的方式。
  • 通过注入的props.route可以获得当前的路由信息。
  • 注意子路由也为’路径全匹配’,上级路由不可省略,如案例中的’/user’。
    案例
// 子路由渲染案例
import styles from './index.less';
export default function TestCompoent(props: any) {
  return (
    <div className={styles['wrap-box']}>
      <div className={styles['cont-text-box']}>
        <div className={styles['cont-text']}>这是一段描述文字!</div>
      </div>
      {/* 子路由渲染 */}
      <div className={styles['router-view-box']}>{props.children}</div>
    </div>
  );
}
 

四、路由对象

import { history, useLocation } from 'umi';
import React from 'react';

const Index = ({ route,location }) => {
  // 路由跳转操作
  history.push('/');
  // 参数获取
  const locationHooks = useLocation();
  // route对象
  console.log(route);
  // location对象
  console.log(location);
  return ( <div>Demo</div> );
}

export default Index;

五、页面keep-alive

安装插件:npm i umi-plugin-keep-alive --save

import { KeepAlive, history } from 'umi';
import PageComponen from './PageComponent';

export default (props) => {
    return (
        <KeepAlive
            id={history.location.pathname} // 根据参数去缓存,如果参数不同就缓存多份,如果参数相同就使用同一个缓存。这样解决了传参改变时,页面不刷新的问题
            when={() => {
                return history.action !== 'POP'; //true卸载时缓存,false卸载时不缓存
            }}
        >
            {/* 页面组件 注意透传props */}
            <PageComponen {...props} />
        </KeepAlive>
    );
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值