AbstractHistory
class AbstractHistroy extends Histroy{
index;
stack;
constructor(router,base){
super(router,base)
// Store routes
this.stack = []
this.index = -1
}
push(location,onComplete,onAbort){
// Perform routing guard events/component rendering, etc
this.transitionTo(location,route= > {
// Update the routing stack
this.stack = this.stack.slice(0.this.index + 1).concat(route)
this.index ++
onComplete && onComplete(route)
},onAbort)
}
replace(loaction,onComplete,onAbort){
// Perform routing guard events/component rendering, etc
this.transitionTo(location,route= > {
// Replace the current route
this.stack = this.stack.slice(0.this.index).concat(route)
onComplete && onComplete(route)
},onAbort)
}
go(n){
const targetIndex = this.index + n
// The route with an empty stack or jump does not exist
if(targetIndex < 0 || targetIndex >= this.stack.lenth){
return
}
// Get the route to jump to
const route = this.stack[targetIndex]
// Perform route guard/component resolution
this.confirmTransitionTo(route,() = > {
const prev = this.current
this.index = targetIndex
// Update route/update index/ execute route post guard
this.updateRoute(route)
this.router.afterHooks.forEach(hook= > {
hook && hook(route,prev)
})
}, err= > {
// Check whether the route is switched
if(isNavigationFailure(err,NavigationFailureType.duplicated)){
this.index = targetIndex
}
})
}
// Get the current path
getCurrentLocation(){
const current = this.stack[this.stack.length - 1]
return current ? current.fullPath: '/'
}
ensureURL(){}}Copy the code