封装方法 utils.js
//只执行一次type就传递once
export function routeParams(queryParams, type = 'once') {
const params = JSON.parse(localStorage.getItem('jumpParams'))
if (params) {
for (const key in params) {
if (Object.hasOwnProperty.call(params, key)) {
const element = params[key];
queryParams[key] = element
}
}
}
if (type === 'once') {
localStorage.removeItem('jumpParams')
}
}
使用方法
全局注册
import { onceRouteParams } from "@/utils/T_utils";
app.config.globalProperties.onceRouteParams = onceRouteParams; //传递参数只执行一次
1.需要跳转的页面
const params = {
key: value
}
localStorage.setItem('jumpParams', JSON.stringify(params))
router.push({ path: '路由'})
2.需要接收的页面
proxy.onceRouteParams(queryParams.value)