Skip to content

How to use client function in pinia store #219

Answered by manchenkoff
JoshPJackson asked this question in Q&A
Discussion options

You must be logged in to vote

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. Since nuxt-auth-sanctum module requires Nuxt context to be present, you should move variable instantiation inside of the store, something like this:

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}
})

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by manchenkoff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants