Skip to content
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

[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables. #27235

Closed
xuzhihui123 opened this issue May 15, 2024 · 6 comments

Comments

@xuzhihui123
Copy link

Environment

"nuxt": "3.11.2"

Reproduction

https://stackblitz.com/edit/github-469cia-fcqpq7?file=plugins%2Ftest.ts

Describe the bug

I introduced a composables method in plugins, which simulates asynchronous requests, but after an asynchronous request, an error will be reported.
Snipaste_2024-05-16_01-03-30

Additional context

No response

Logs

No response

Copy link

stackblitz bot commented May 15, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@arashsheyda
Copy link
Member

arashsheyda commented May 15, 2024

@xuzhihui123 composables are runtime only, if you want to use a composable in plugin you have to make sure it's running on client side only. you can achive that by adding .client to your plugin name (test.client.ts) or wapping it around onNuxtReady

onNuxtReady(async () => {
    await a();
})

@xuzhihui123
Copy link
Author

thanks, Is there any other way to run it on the server side?

@xuzhihui123
Copy link
Author

Because I want to set some values after requesting some data from the server, which happens in server-side rendering, not just client-side rendering

@huang-julien
Copy link
Member

Hello, you need to set the nuxt context with nuxtApp.runWithContext. useState is a composable that requires the nuxt context to be accessible

see https://nuxt.com/docs/guide/going-further/nuxt-app#the-nuxt-context

@huang-julien huang-julien closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
@xuzhihui123
Copy link
Author

ok, solved it,thanks

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

No branches or pull requests

3 participants