vuejs / vue-router

🚦 The official router for Vue 2
http://v3.router.vuejs.org/
MIT License
18.99k stars 5.06k forks source link

请在vue-router3版本中加入useRoute和useRouter钩子函数。 #3889

Closed qew4 closed 3 months ago

qew4 commented 3 months ago

Version

3.6.5

Steps to reproduce

因为vue2.7是支持组合式API的,但是又只支持vue-router3,vue-router3 又没有实现useRoute和useRouter钩子函数。这就导致我们自己实现的钩子函数useRoute和useRouter无法在watch中被监听到变化,主要是useRoute().query的变化。 我们自己封装的钩子如下: import { getCurrentInstance } from 'vue' /* this.$route替换方案 / export function useRoute() { const { proxy } = getCurrentInstance() const route = proxy.$route return route } /* this.$router替换方案 / export function useRouter() { const { proxy } = getCurrentInstance() const router = proxy.$router return router }

What is expected?

想要watch能监听到useRoute().query的变化。 在vue-router3中加入useRoute和useRouter钩子函数。

What is actually happening?

watch能不能监听到替代方案useRoute().query的变化。

posva commented 3 months ago

They are already there:

import { useRouter } from 'vue-router/composables'

https://github.com/vuejs/vue-router/blob/dev/src/composables/index.js

qew4 commented 1 month ago

excellent! import { useRouter } from 'vue-router/composables' it can be watch and work.