• 总结改变和获取 url 的方法 (包括 umi,react-router,原生)


    原生: 

    背景 : location.search,但返回的是一个?xxx=aa&yyy=bb 这种形式,并不能供我们正常调用,通常我们可能会用正则进行进一步截取,

    快速获取当前页面的参数 : 
    方法一:
    1. location.href = 'http://www.baidu.com?ddd=1&fff=2'
    2. const querySearch = location.search // ?ddd=1&fff=2
    3. const queryParams = new URLSearchParams(querySearch) // 得到URLSearchParams解析对象
    4. const result = Object.fromEntries(queryParams.entries()) // { ddd: 1, fff: 2 }
    方法二:
    1. const params = new Proxy(new URLSearchParams(window.location.search), {
    2. get: (searchParams, prop) => searchParams.get(prop),
    3. });
    4. console.log(params) // 得到一个Proxy对象
    5. console.log(params.ddd) // 1

    封装成一个hook

    1. const useParams = (urlSearch?) => {
    2. const params = useRef(
    3. new Proxy(new URLSearchParams(urlSearch || window.location.search), {
    4. get: (searchParams, prop) => searchParams.get(prop),
    5. })
    6. );
    7. return params.current
    8. }

    Umi:

    useParams

    useParams 钩子函数返回动态路由的匹配参数键值对对象;子路由中会集成父路由的动态参数。

    1. import { useParams } from 'umi';
    2. // 假设有路由配置 user/:uId/repo/:rId
    3. // 当前路径 user/abc/repo/def
    4. const params = useParams()
    5. /* params
    6. { uId: 'abc', rId: 'def'}
    7. */

    跳转/获取路由 : 

    1. import { history,history } from 'umi';
    2. // 正确的在组件或页面中执行路由导航
    3. const handleClick = () => {
    4. history.push('/app-settings/popular', { data: '这是一些状态数据' });
    5. };
    6. // 通过 history.location.query 获取
    7. if (history.location.query?.id || history.location.query?.draftId)

    获取路由参数:

      const state: any = useLocation().state // 获取上面方法的数据

  • 相关阅读:
    form表单的自定义校验规则
    JVM——JVM概述以及双亲委派机制
    sap 进行dubgger看表数据
    Java面试被问框架源码看过吗?70道SSM面试题及学习笔记值得收藏!
    v-cloak的作用和原理
    ALINX_ZYNQ_MPSoC开发平台FPGA教程:PL的点灯实验
    NPDP|什么样的产品经理可以被称为优秀?
    自动驾驶系统激光雷达传感器反射率标定板
    EN 14967:防水沥青防潮层—CE认证
    修改树莓派4b密码
  • 原文地址:https://blog.csdn.net/weixin_43416349/article/details/133811413