nuxt / nuxt

The Intuitive Vue Framework.

Home Page:https://nuxt.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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`.

xuzhihui123 opened this issue · comments

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

@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();
})

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

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