vue:监听路由的变化

1.vue3

import { useRouter} from 'vue-router'

const router = useRouter()
// 监听当前路由
watch(
  () => router.currentRoute.value,
  (newValue: any) => {
    console.log('newValue',newValue)
  },
  { immediate: true }
)

2.vue2

watch: {
  $route: {
    handler: function(newRoute, oldRoute){
      console.log('当前路由:', newRoute);
      console.log('之前的路由:', oldRoute);
    },
    // 深度观察监听
    deep: true
  }
}

3.获取hash值:window.location.hash()

你可能感兴趣的:(vue.js,javascript,前端)