Use pinia outside components reusing the code #1158
Unanswered
efimatica
asked this question in
Help and Questions
Replies: 1 comment
-
@efimatica, I am not sure if this is you wanted. sample code for reusable service within pinia store: export const CounterService = () => {
const counterStore = useCounterStore();
// reusable method
const ajax = () => {
return Promise.resolve(counterStore.counter);
};
return {
ajax,
};
};
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I followed the
services
pattern so I have all the interactions with the API in isolated javascript files, like:multimediaService.js
As you can see I need the propertyId variable which is stored on a pinia store.
In my use cases, almost for every API request I need that variable.
So my idea was do the following:
But this 2 lines are failing because I'm using the store before App created Pinia.
If I move these 2 lines inside the
useMultimediaCreateService
works because this function is called after App creates Pinia, a SFC will import that service and call it.The thing is, I have a lot of thin methods like
useMultimediaCreateService
so I cannot duplicate the code and paste these 2 lines of every method...I can implement a method to extract that
propertyId
variable but I will have to call this method on all methods inside services files and it's a bit frustrating 😢Is there another way to achieve that I want?
Beta Was this translation helpful? Give feedback.
All reactions