Reusable logic in pinia store #2496
Replies: 2 comments
-
You can use classes but you will need to add custom code to handle serialization while regular objects need not. Another option for you is to return an extended computed of the student and parent that is augmented with the extra properties: defineStore('...', () => {
const parent = ref()
const parentExtended = computed(() => parent.value && ({
...parent.value,
avatarURL: ...,
}))
return { _parent: parent, parent: parentExtended }
}) It's important you return the original state in one way or another so pinia can recognize it as state. |
Beta Was this translation helpful? Give feedback.
-
To address the problem of extending or composing Pinia setup stores for reusable getters, setters, and actions, you can utilize the Here's how you can achieve this using
First, you need to install the npm install epps
import { ref, computed } from 'vue';
}
});
Use the import { defineStore } from 'pinia'; const usePersonStore = defineStore('person-store', () => {
}); export const useOtherStore = defineStore('other-store', () => {
}); By using the For more information, you can check out the |
Beta Was this translation helpful? Give feedback.
-
Hi! I have pinia store where I have student & parent
Both of them are users
Both students and parents have
avatarUrl
(have to be computed based on gender) andfullName
(computed based on name and surname properties).Where do I put these getters?
parentAvatarUrl
andstudentAvatarUrl
)helper
? (But using it like sogetAvatarUrl(parent)
feels like helpers know too much)User class
and make the getters to be methods of the class, but googling makes me think that reactivity doesnt work well with class objects. or am I wrong?)The option that seems cleaner to me is class based.
So my second question is: in js world people seem to use classes less than in PHP. But where do you put methods then? How do you approach having object specific logic, when those objects also have to be reactive?
Any help much appreciated. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions