Skip to content

How inject dependency to store? #388

Closed Answered by posva
norvele asked this question in Help and Questions
Mar 9, 2021 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

right now you can use pinia.use() but I plan on redesigning the API to make it more flexible. For example, with mande, you can register a mande instance like this:

const api = mande('/api')

// this line will be different in future versions and it will be documented too
pinia.use(() => ({ api })

defineStore({
  id: 'a',
  state: () => ({ users: [] })
  actions: {
    async fetchStuff() {
      this.users = await this.api.get('/users')
     }
  }
})

// for ts
import 'pinia'
declare module 'pinia' {
  import { MandeInstance } from 'mande'
  export interface PiniaCustomProperties {
    api: MandeInstance
  }

This allows you to attach headers with Bearer tokens to the api objects and use th…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@knightazura
Comment options

@posva
Comment options

@LandSprutte
Comment options

@karmats
Comment options

@tabun-matadorov
Comment options

Answer selected by norvele
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants