/**
* 功能: 重写vue的push和replace方法,并可以集中处理报错
* @param {VueRouter} VueRouter - 需要传入vue的router-dom
* @param {function} [push_callback] - 已经重写的push方法,但是可以对失败函数做出单独处理,default is empty function
* @param {function} [replace_callback] - 已经重写的replace方法,但是可以对失败函数做出单独处理,default is empty function
* @returns {undefined} - undefined
*/
function vue_router_push_replace(
VueRouter,
push_callback = function () {},
replace_callback = function () {}
) {
if (VueRouter) {
const originPush = VueRouter.prototype.push; //先把原来的push函数保存一份
const originReplace = VueRouter.prototype.replace; //先把原来的replace函数保存一份
VueRouter.prototype.push = function (location, resolved, rejected) {
if (resolved === undefined && rejected === undefined) {
return originPush.call(this, location).catch(push_callback);
} else {
return originPush.call(this, location, resolved, rejected);
}
};
VueRouter.prototype.replace = function (location, resolved, rejected) {
if (resolved === undefined && rejected === undefined) {
return originReplace.call(this, location).catch(replace_callback);
} else {
return originReplace.call(this, location, resolved, rejected);
}
};
} else {
console.error(
new Error(
"vue_router_push_replace: frist parameter " +
"is " +
"no," +
" you is sb"
)
);
}
}
export default vue_router_push_replace;