动态路由

router

  1. path: '/assets-manage/edit/:fundIdentifier',

传参

  1. import { useHistory, useLocation } from 'react-router-dom'
  2. const { pathname } = useLocation()
  3. history.push(`${pathname}/edit/${fundIdentifier}`)

获取

  1. import { useParams } from 'react-router-dom'
  2. const { fundIdentifier } = useParams()

query 参数

pathname + query

  1. <Link to={{ pathname: '/marketings/sms/new', query: { msgType: 0 } }} >普通短信</Link>

获取

  1. import { useLocation } from 'react-router-dom';
  2. console.log(useLocation());

image.png
这种方式,刷新页面后参数丢失

直接拼接

直接拼接的方式,刷新页面后参数不会丢失

携带

  1. <Link to={`/marketings/sms/new?MsgType=0`}>普通短信</Link>

获取

  1. import { useLocation } from 'react-router-dom';
  2. console.log(useLocation());

image.png

  1. export const getUrlParams = search => {
  2. const query = search || window.location.search
  3. if (Object.is(query, '')) {
  4. return null
  5. } else {
  6. let json = {}
  7. let arr = query.slice(1).split('&')
  8. for (let i = 0; i < arr.length; i++) {
  9. let innerArr = arr[i].split('=')
  10. json[innerArr[0]] = innerArr[1]
  11. }
  12. return json
  13. }
  14. }

注意:需要使用 useLocation 下的 search, window.location 下 search 为空。