【vue3】获取当前页面路由


有三种方法

  1. 第一种
import {
     defineComponent, ref, getCurrentInstance } from 'vue';

const {
     proxy }: any = getCurrentInstance();
console.log(proxy.$router.currentRoute.value);

 

  1. 第二种
import {
     defineComponent, ref, toRaw} from 'vue';
import {
     useRouter } from 'vue-router';

let router = useRouter()
console.log(toRaw(router));

 

  1. 第三种
import {
     defineComponent, ref, watch } from 'vue';
import {
     useRouter } from 'vue-router';

   let router = useRouter()
   watch(
      () => router,
      (newValue, oldValue) => {
    
        console.log(newValue.currentRoute['_value'].fullPath);
      },
      {
     immediate: true }
    );
 //在选项参数中指定 immediate: true 将立即以表达式的当前值触发回调:

模板中直接使用

详细参考

Vue3.0学习 - 第十七节,vue3中如何使用 router、 watch、computed、和路由监听 - 代码先锋网 (codeleading.com)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM