【vue3】监听路由变化

有时在页面刷新或者返回等操作时,想监听路由变化进行数据更新等操作。那么在vue3中怎么对路由进行监听呢?这里为大家提供了这两种方法:

  1. onBeforeRouteUpdate路由守卫
import {
    
     useRouter,onBeforeRouteUpdate } from 'vue-router';

let router = useRouter()
onBeforeRouteUpdate((to) => {
    
    
        // console.log('onBeforeRouteUpdate',to.path);     
 });
  1. watch监听
watch(() =>router.currentRoute.value.path,(newValue,oldValue)=> {
    
    
        console.log('watch',newValue);
},{
    
     immediate: true })

猜你喜欢

转载自blog.csdn.net/qq_38974163/article/details/122187858