How inject dependency to store? #388
-
I want to inject Service\Client (fetching from apis) to store. How can i do this? Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Mar 9, 2021
Replies: 1 comment 5 replies
-
right now you can use 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 If you don't have SSR or stateful requests (e.g. bearer tokens, cookies), then just import whatever you need: import axios from 'axios'
defineStore({
id: 'a',
state: () => ({ users: [] })
actions: {
async fetchStuff() {
this.users = await axios.get('/api/users')
}
}
}) |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
norvele
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 amande
instance like this:This allows you to attach headers with Bearer tokens to the
api
objects and use th…