Hen*_*Jan 4 vue.js vue-router vuejs3 vue-composition-api vue-router4
我正在尝试"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)
Run Code Online (Sandbox Code Playgroud)
但在 Vue3 中无法使用ctx.root(如 Evan You 此处所述)。
那么使用 Vue3 获取当前路由的首选方法是什么?
Bou*_*him 11
您可以使用可组合功能useRoute:
import {useRoute} from 'vue-router'
import {computed} from 'vue'
...
setup(){
const route=useRoute();
const path = computed(() =>route.path)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2128 次 |
| 最近记录: |