How to use client function in pinia store #219
-
I have a pinia store in which I would like to retrieve some models from my api. Within the store actions I have a method to get all pages:
However when loading the page in the browser I see this in the console: Is there a nice way of solving this issue or do I just need to move the api calls into the components? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @JoshPJackson! Thanks for the question. Indeed you can use import { defineStore } from 'pinia'
export const usePageStore = defineStore('myPageStore', () => {
const client = useSanctumClient()
async function get(id: string) {
return await client('/api/pages/' + id)
}
async function getAll() {
return await client('/api/pages')
}
return {get, getAll}
}) |
Beta Was this translation helpful? Give feedback.
Hey @JoshPJackson! Thanks for the question. Indeed you can use
useSanctumClient
in your Pinia store, but it looks like that the scope of the call is incorrect. Sincenuxt-auth-sanctum
module requires Nuxt context to be present, you should move variable instantiation inside of the store, something like this: