6

我正在尝试"https://example.com/some/path"使用 Vue3 和 Vue-router 获取当前路径(类似于 )。

之前,在使用 Vue2 时,我可以使用以下方法获取当前路线:

    // works with vue 2.x with composition-api, but not with vue 3.x
    setup(_, ctx) {
        const path = computed(() => ctx.root.$route.path)

但在 Vue3 中无法使用ctx.root如 Evan You 所述)。
那么用 Vue3 获取当前路由的首选方法是什么?

4

1 回答 1

19

您可以使用可组合功能useRoute

import {useRoute} from 'vue-router'
import {computed} from 'vue'
...

setup(){
   const route=useRoute();

   const path = computed(() =>route.path)
}

于 2021-02-01T08:43:05.573 回答