我是这么写的

import {computed,onMounted,ref,getCurrentInstance, ComponentInternalInstance } from "vue";
import { useRoute } from 'vue-router';
const route = useRoute();

const login=()=>{

if(thisRoute=="/home/index"){ 
   const { proxy } = getCurrentInstance() as ComponentInternalInstance;
   proxy!.$forceUpdate()
}
}

最后发现

const { proxy } = getCurrentInstance() as ComponentInternalInstance;这一行代码应该提到函数外面。最外层获取再在函数里面用proxy才行。改成这样

import {computed,onMounted,ref,getCurrentInstance, ComponentInternalInstance } from "vue";

 const { proxy } = getCurrentInstance() as ComponentInternalInstance;

import { useRoute } from 'vue-router';
const route = useRoute();

const login=()=>{

if(thisRoute=="/home/index"){ 
  
   proxy!.$forceUpdate()
}
}

同时和这个一样的还有useRoute()。也是要先定义在外层。

如果你这样写函数里面也会报错

index.vue:83 Uncaught ReferenceError: Cannot access 'route' before initialization
    at index.vue:83:19

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐