方式一:通过onBeforeRouteUpdate钩子
onBeforeRouteUpdate((to, from) => {
const toRouteIndex = to.meta.index as number
const fromRouteIndex = from.meta.index as number
if (toRouteIndex > fromRouteIndex) {
transitionName.value = 'slide-left'
} else {
transitionName.value = 'slide-right'
}
})
方式二:组件内部通过beforeRouteLeave钩子
beforeRouteLeave(to, from, next){
if(from.path=='/b'){
next({replace: true,redirect: '/a'});
}else {
next()
}
}
方式三:通过watch监听
watch(() => route.path,(newPath, oldPath) => { console.log(newPath) },{ immediate: true });