[英]Pinia: Cannot access store from Nuxt3 API
我在server/api/<api-endpoints>
中有一個帶有 Api 的 Nuxt3 項目。 在端點內,當我嘗試使用 Pinia Store 時,它說:
[nuxt] [request error] []: getActivePinia was called with no active Pinia. Did you forget to install pinia?
或者當我嘗試從可組合項訪問可組合項composables
時,它無法定義任何變量(說 xxx 未定義,即使它在組件中工作,所以它無法在函數中定義任何內容)。
server/api/token
中的 Pinia 示例
import { useAuthStore } from "../stores/useAuthStore";
export default async (req: any, res: any) => {
const authStore = useAuthStore()
return authStore.access_token
};
server/api/user
中的可組合示例
import { useUserLogin } from "../../composables/useUserLogin";
export default async (req: any, res: any) => {
const userLogin = useUserLogin()
return twitchLogin.is_logged_in
};
我通過將 buildModules 重命名為nuxt.config.js中的模塊來解決它
Nuxt模塊現在僅限構建時使用,Nuxt 2 中使用的buildModules屬性已棄用,取而代之的是模塊。
// nuxt.config.js
export default defineNuxtConfig({
modules: [
// ...
'@pinia/nuxt',
],
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.