在Vue3的項目中,我想進行路由跳轉,於是使用
this.$router.push("/newCollect")
報Cannot read property '$router' of undefined錯誤。
原來該語法只能用於method中,而我是在setup函數中進行路由跳轉的。
解決方法為:
1.導入useRouter函數
import { useRouter } from "vue-router";
2.在進入setup函數時執行
const router = useRouter()
3.在setup函數中進行路由跳轉
不傳參寫法為:
router.push('/newCollect')
傳參寫法為:
router.push({ path: "/newCollect", query: { mode: "edit", }, });
4.在新頁面獲取參數(需要傳參時)
const mode = router.currentRoute.value.query.mode;
