vue3的setup中使用路由

阅读 93

2021-09-23

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
        }
    }
} 
  1. 或者使用useRouter
import { useRouter } from 'vue-router'
export default {
    setup() {
        const router = useRouter();
        function goTo() { 
            router.push({ path: '/home',  query: { ..... } });
        } 
        return {
            goTo
        }
    }
} 

精彩评论(0)

0 0 举报