0
点赞
收藏
分享

微信扫一扫

vue3的setup中使用路由

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 条评论