umi4 history.push的參數(shù)對(duì)比umi3 的變化

image.png

在umi3里面,跳轉(zhuǎn)時(shí)傳參數(shù)寫(xiě)法

// 帶參數(shù)跳轉(zhuǎn)到指定路由
history.push('/list?a=b');
history.push({
  pathname: '/list',
  query: {
    a: 'b',
  },
});

// 跳轉(zhuǎn)到上一個(gè)路由
history.goBack();

在umi4里面,跳轉(zhuǎn)時(shí)傳參數(shù)寫(xiě)法

// 帶參數(shù)跳轉(zhuǎn)到指定路由
// const state = { a: 1 }
history.push('/list?a=b&c=d#anchor', state);

// 帶參數(shù)跳轉(zhuǎn)到指定路由
history.push('/list?a=b&c=d#anchor');
history.push({
  pathname: '/list',
  search: '?a=b&c=d',
  hash: 'anchor',
  state: { a: 1 } // state地址欄不顯示,刷新后不消失
});

// 跳轉(zhuǎn)當(dāng)前路徑,并刷新 state
history.push({}, state)

// 跳轉(zhuǎn)到上一個(gè)路由
history.back();
history.go(-1);
umi4和 history 相關(guān)的操作,用于獲取當(dāng)前路由信息、執(zhí)行路由跳轉(zhuǎn)、監(jiān)聽(tīng)路由變更。
// 建議組件或 hooks 里用 useLocation 取
import { useLocation } from 'umi';
export default function Page() {
  let location = useLocation();
  return (
    <div>
     { location.pathname }
     { location.search }
     { location.hash }
     { location.state }
    </div>
  );
}

// 或者
import { history } from 'umi'
export default function Page() {
  return (
    <div>
      { history.location.pathname }
      { history.location.search}
      { history.location.state}
      ...
    </div>
  );
}

大部分內(nèi)容源自官網(wǎng),可自行去umi3和umi4查看,因貼了官網(wǎng)地址導(dǎo)致文章被鎖,已將地址去掉

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容