1.通过getCurrentInstance,获取vue 实例,之后在根节点$root中获取router
import { onMounted, getCurrentInstance } from "vue";
export default {
setup() {
const { ctx } = getCurrentInstance();
function goTo() {
ctx.$router.push({ path: '/home', query: { ..... } });
}
return {
goTo
}
}
}
- 或者使用useRouter
import { useRouter } from 'vue-router'
export default {
setup() {
const router = useRouter();
function goTo() {
router.push({ path: '/home', query: { ..... } });
}
return {
goTo
}
}
}