Skip to content

Add onBeforeRouteEnter API to the list of composition API #1517

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

Closed
ahnpnl opened this issue Aug 14, 2022 · 6 comments
Closed

Add onBeforeRouteEnter API to the list of composition API #1517

ahnpnl opened this issue Aug 14, 2022 · 6 comments

Comments

@ahnpnl
Copy link

ahnpnl commented Aug 14, 2022

What problem does this feature solve?

Current the documentation about composition API https://router.vuejs.org/guide/advanced/navigation-guards.html#using-the-composition-api doesn't mention about onBeforeRouteEnter. I see in Options API, there is beforeRouteEnter hence there should be one for composition API too.

What does the proposed API look like?

export function onBeforeRouteUpdate(to, from) {
      //....
}
@posva
Copy link
Member

posva commented Aug 16, 2022

onBeforeRouteEnter() doesn't exist, it's not possible to implement such guard with the composition api.

@posva posva closed this as completed Aug 16, 2022
@a798032825
Copy link

onBeforeRouteEnter() doesn't exist, it's not possible to implement such guard with the composition api.

hello,may I get a case to put beforeRouteEnter into <script setup>.
just as onBeforeRouteleave can do.

@719media
Copy link

719media commented Jun 23, 2023

@posva Can you help us understand a little more on this? I'm confused because beforeRouteEnter exists in options API here: https://router.vuejs.org/guide/advanced/navigation-guards.html#using-the-composition-api

Not sure why it then couldn't be made to be used inside of setup() ? Could it be done in a PR, or is there a reason why it hasn't been done?

Copy link
Member

posva commented Jun 23, 2023

It’s a technical limitation: setup runs when the component is created which happens after the navigation. Therefore you cannot add a navigation guard while the component is created

@taras-turchenko-moc
Copy link

@posva Hi! Can we do onBeforeRouteEnter hook using compiler macros? We can add this hook using defineOptions so I believe it's possible to make some shorthand for this to align options and composition apis

defineOptions({
    beforeRouteEnter: preloadGuard(() => loadInitial())
});

Also it would be nice to add route meta from component like nuxt doing

@posva
Copy link
Member

posva commented Aug 1, 2024

The prefered direction are data loaders: https://uvr.esm.is/data-loaders/. You also have a definePage() macro for meta

curtgrimes added a commit to curtgrimes/vue-router that referenced this issue Apr 14, 2025
Upgrade the playground package to use the `<script setup>` syntax and
TypeScript. This change makes the playground more consistent with recent
Vue.js practices and improves type safety.

Keep the purpose of each example identical to the original examples as
much as possible. Therefore, components using `beforeRouteEnter()`,
[which does not have a `<script setup>`
equivalent](vuejs#1517), are not
converted to `<script setup>` syntax but are only converted to use
TypeScript for improved type safety (Home.vue and
ComponentWithData.vue). Converting these to use the [recommended data
loaders or
`defineMacro()`](vuejs#1517 (comment))
could be an option in the future.

Update `RouteNamedMap` in main.ts to improve type safety and better
demonstrate the newer typed routes feature (introduced in
[email protected]) in LongView.vue, Nested.vue, and RepeatedParams.vue.

Fix type issue in AppLink.vue where `RouterLinkProps` replaces
`RouterLink.props`.

Remove the `name` option from the `defineComponent` calls that used it,
because this option is [only useful when the component recursively calls
itself](https://v2.vuejs.org/v2/api/#name),
but there are not components in this playground project that do so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants