-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<router-view> add matchedRoute as v-slot param #1407
Comments
A workaround for those who need it "right now": <script setup lang="ts">
import type { VNode } from 'vue'
import { useRouter } from 'vue-router'
const normalizedRoutes = getRoutes()
const getMatchedRoute = (component: VNode) =>
component &&
normalizedRoutes.find(({ components }) => components.default === component.type)
</script>
<template>
<router-view v-slot="{ Component }">
<h3>{{ getMatchedRoute(Component)?.meta.title }}</h3>
<component :is="Component"></component>
<router-view></router-view>
</router-view>
</template> |
The information that you can get is the matched route record but there isn't a matched route per nested route, there is always one single matched route and this is intentional. To access the matched route record anywhere you need to index the import { viewDepthKey } from 'vue-router'
// within the setup of a component
const depth = inject(viewDepthKey)!
const route = useRoute()
// this could be a computed
route.matched[depth] Accessing the route records is quite advanced functionality so I would be against exposing them easily to avoid confusion among users. As shown, it's still available for advanced use cases though. Do you have any use case that cannot be achieved with the example above? There is also #1217 (comment). |
Ok, index the |
The depth is a low-level API, it would be confusing to expose it. Is there anything worth accessing this way besides |
I'm surprised with RFC recommendation for so trivial use case, I just trying to resolve an issue which exists (right now, and makes life harder). Don't really understand why you going to resolve a ghost-issue that could happen (somewhere somebody sometimes can feel confused). |
It's okay if you don't want to do an RFC. To me, it's important to add APIs that are not possible otherwise, solve common problems, and cater to a good amount of use cases (not just one). The RFC allows finding that balance. It also allows checking for possible edge cases as I mentioned above. Closing this in favor of #1408 |
Thank you for your time. For now this workaround doesn't works as But simpler solution seems to be import { inject } from 'vue'
import { matchedRouteKey} from 'vue-router'
const matchedRoute = inject(matchedRouteKey) |
What problem does this feature solve?
In case we have nested routers it's needed to get matched route object for every view. While children route is active, there is no official way to know what exactly matched as parent route, only it's component:
What does the proposed API look like?
matchedRoute
param passed withv-slot
in<router-view>
The text was updated successfully, but these errors were encountered: