import { computed } from '@vue/composition-api' ; import type { Ref } from '@vue/composition-api' ; import type { Context } from '@nuxt/types' ; import type { Route } from 'vue-router' ; import { globalNuxt } from '@nuxtjs/composition-api/dist/runtime/globals' ; import { getCurrentInstance } from './utils' ; interface ContextCallback { ( context ) : void ; } deprecated export const withContext = ( callback ) => { const vm = getCurrentInstance ( ) ; if ( ! vm ) throw new Error ( 'This must be called within a setup function.' ) ; callback ( ( vm [ globalNuxt ] || vm . $options ) . context ) ; } ; interface UseContextReturn extends Omit < Context , 'route' | 'query' | 'from' | 'params' > { route : Ref < Route > ; query : Ref < Route [ 'query' ] > ; from : Ref < Context [ 'from' ] > ; params : Ref < Route [ 'params' ] > ; } example export const useContext = ( ) : UseContextReturn => { const vm = getCurrentInstance ( ) ; if ( ! vm ) throw new Error ( 'This must be called within a setup function.' ) ; return { ... ( vm [ globalNuxt ] || vm . $options ) . context , deprecated route : computed ( ( ) => vm . $route ) , deprecated query : computed ( ( ) => vm . $route . query ) , deprecated from : computed ( ( ) => ( vm [ globalNuxt ] || vm . $options ) . context . from ) , deprecated params : computed ( ( ) => vm . $route . params ) , } ; } ;