Skip to content
Discussion options

You must be logged in to vote

You can also put those two lines of code into its own composable if it’s used everywhere. If your $api is implemented using inject provide, you will have to inject it unfortunately
Query options can be called anywhere so they can’t use injections:

export const projectsQuery = defineQueryOptions(($api: ReturnType<typeof useRawAPI>['$api']) => ({
  key: ['projects'],
  query: () => {
    return $api('/api/projects');
  },
  staleTime: DEFAULT_STALE_TIME,
}));

export function useProjectsQuery() {
	const { $api } = useRawAPI()
	return useQuery(projectsQuery($api))
}

another option not always possible is using a global (not safe for SSR)

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@amandesai01
Comment options

@posva
Comment options

posva Oct 7, 2025
Maintainer

Answer selected by posva
@husayt
Comment options

@posva
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants