Skip to content

Commit

Permalink
docs: allow global injections
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 11, 2023
1 parent 0d0c915 commit f828a14
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/docs/guide/advanced/navigation-guards.md
Expand Up @@ -134,6 +134,21 @@ router.afterEach((to, from, failure) => {

Learn more about navigation failures on [its guide](./navigation-failures.md).

## Global injections within guards

Since Vue 3.3, it is possible to use `inject()` within navigation guards. This is useful for injecting global properties like the [pinia stores](https://pinia.vuejs.org). Anything that is provided with `app.provide()` is also accessible within `router.beforeEach()`, `router.beforeResolve()`, `router.afterEach()`:

```ts
// main.ts
const app = createApp(App)
app.provide('global', 'hello injections')

// router.ts or main.ts
router.beforeEach((to, from) => {
console.log(inject('global')) // -> 'hello injections'
})
```

## Per-Route Guard

You can define `beforeEnter` guards directly on a route's configuration object:
Expand Down

0 comments on commit f828a14

Please sign in to comment.