1. 程式人生 > 其它 >Vue路由跳轉攜帶固定引數

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",
        })

    }

})