在引用了umi的情況下,可以使用umi中的useLocation來獲取當前url地址中的某個變量,
例如
import { useLocation } from 'umi'
const A = () => {
const location = useLocation();
return (
<div>
{location.query.變量名}
<div>
)
}
當然,也可以自己寫函數,來達到這個目的:
const getQuery = (name) =>{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substring(1).match(reg);
if(r!=null)return decodeURI(r[2]); return null;
}
getQuery('變量名')