Skip to content

docs: using beforeRouteEnter with defineOptions #2131

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/docs/guide/advanced/composition-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ export default {

Composition API guards can also be used in any component rendered by `<router-view>`, they don't have to be used directly on the route component like in-component guards.

There isn't an equivalent Composition API function for `beforeRouteEnter`. The `beforeRouteEnter` guard needs to run before `setup`, so calling a function inside `setup` would be too late. If you're using `<script setup>`, you can specify the `beforeRouteEnter` option using the [`defineOptions`](https://vuejs.org/api/sfc-script-setup.html#defineoptions) macro:

```vue
<script setup>
// Using defineOptions requires Vue 3.3+
defineOptions({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure about two things:

  • The example uses defineOptions() so it makes it look like you can use setup stuff inside beforeRouteEnter()
  • I would not recommend using this navigation guard at the moment as I plan data loaders to be a much better approach to the same problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The potential confusion around variable scope is a really good point and not something I'd considered. While this is still something I'd be happy to do myself, it does muddy the waters from a docs perspective.

beforeRouteEnter() {
// ...
}
})
</script>
```

For versions of Vue prior to 3.3, a [separate `<script>` tag](https://vuejs.org/api/sfc-script-setup.html#usage-alongside-normal-script) can be used instead of `defineOptions`.

## `useLink`

Vue Router exposes the internal behavior of RouterLink as a composable. It accepts a reactive object like the props of `RouterLink` and exposes low-level properties to build your own `RouterLink` component or generate custom links:
Expand Down