做网站的标准百度的域名
Vue Router 的push方法依赖this
Vue Router 的push方法内部通过this访问关键属性(如this.history):
// Vue Router内部简化实现
VueRouter.prototype.push = function(location) {
// 依赖this访问路由历史和配置
return this.history.push(location);
};
当你重写push时,若直接调用:
let origin = VueRouter.prototype.push
VueRouter.prototype.push = function (location, reslove, reject) {
if (reslove && reject) {
origin(this,location, reslove, reject)
}
else {
origin(this,location, () => { }, () => { })
}
}
this会指向全局对象(如window)或undefined(严格模式),导致this.history报错。
所以需显式将this绑定为 Vue Router 实例,确保this.history正确指向路由历史。
即:
let origin = VueRouter.prototype.push
VueRouter.prototype.push = function (location, reslove, reject) {
if (reslove && reject) {
origin.call(this,location, reslove, reject)
}
else {
origin.call(this,location, () => { }, () => { })
}
}
