我是这么写的
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