Vue路由跳转携带固定参数

index.js文件修改

import Router from 'vue-router'
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) {
        return originalPush.call(this, location, onResolve, onReject);
    }
    return originalPush.call(this, location).catch((err) => err);
};
Vue.use(Router)

let router = new Router({.....})


import Router from 'vue-router'
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) {
        return originalPush.call(this, location, onResolve, onReject);
    }
    return originalPush.call(this, location).catch((err) => err);
};
Vue.use(Router)


 let router = new Router({.....})


router.beforeEach((to, from, next) => {
    if (to.path == '/404' || to.query.tc) {
        next();
        return;
    }
    if (from.query.tc && from.query.tc.length <= 32) {
        //如果目标路由没有公共参数,就获取公共参数并添加
        let toQuery = JSON.parse(JSON.stringify(to.query));
        toQuery.tc = from.query.tc;
        next({
            path: to.path,
            query: toQuery
        })

    } else {

        router.push({
            path: "/404",
            name: "404",
        })

    }

}) 

 

posted @ 2022-06-01 11:50  b̶i̶n̶g̶.̶  阅读(503)  评论(0编辑  收藏  举报