vue3 路由跳轉:Cannot read properties of undefined (reading 'push')

在vue2中我們通常會在全局的接口請求裏面設置攔截器,如果登錄失效或者說其他情況需要跳轉到登錄頁面或者說其他頁面的,我們就使用this.$router.push實現
vue3中同樣使用改方式:

import { useRouter } from 'vue-router';

const router = useRouter();

router.push()

但是控制檯會報錯:

Cannot read properties of undefined (reading 'push')

這是英文useRoute, useRouter必須寫到setup中,強行在函數中使用這兩會報undefined,導致無法獲取路由數據和路由方法。

此時要修改引入方式:

import Vrouter from "@/router"、

const router = Vrouter;

router.push()

這樣就沒問題了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章