Skip to content

Commit

Permalink
fix: make route-related context reactive
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `route`, `query`, `from` and `params` are now returned as refs from `useContext`, which was probably what you wanted anyway.
  • Loading branch information
danielroe committed May 14, 2020
1 parent f4f80b4 commit 91292c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/helpers/useContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export default defineComponent({
},
})
```

::: tip
Note that `route`, `query`, `from` and `params` are reactive refs (accessed with `.value`), but the rest of the context is not.
:::
10 changes: 8 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentInstance } from '@vue/composition-api'
import { getCurrentInstance, computed } from '@vue/composition-api'

import type { Context } from '@nuxt/types'

Expand Down Expand Up @@ -35,5 +35,11 @@ export const useContext = () => {
const vm = getCurrentInstance()
if (!vm) throw new Error('This must be called within a setup function.')

return vm.$nuxt.context
return {
...vm.$nuxt.context,
route: computed(() => vm.$route),
query: computed(() => vm.$route.query),
from: computed(() => vm.$route.redirectedFrom),
params: computed(() => vm.$route.params),
}
}

0 comments on commit 91292c8

Please sign in to comment.