import {
useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
router.push({
path: '/activity', query: {
id: 1} })
There is no problem with this introduction in a normal .vue file, but in a js file, it is introduced
import {
useRoute, useRouter } from 'vue-router'
console.log(useRouter())
// 打印结果为undefined
The reason is that the first way to introduce useRouter in vue3 is vue3's hooks, so you need to import the router file you created in the js file.
import router from '@/router/index'
router.push('activity')
// 正常跳转