import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
router.push({ path: '/index'})
这种在正常.vue文件中引入没有任何问题,但在js文件中,引入
import { useRoute, useRouter } from 'vue-router'
console.log(useRouter())
// 打印结果为undefined
控制台警告[Vue warn]: inject() can only be used inside setup() or functional components.
原因是,在vue3中第一种useRouter的引入方式是vue3的hooks,因此在js文件中需要导入你创建的router文件即可
import router from '@/router/index.js'
router.push({ path: '/index'})
// 正常跳转
标签:文件,vue,js,vue3,router,useRouter
From: https://www.cnblogs.com/echohye/p/17231728.html