How to work properly with Getters? #1659
Unanswered
Asored-D
asked this question in
Help and Questions
Replies: 0 comments
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.
-
Hi guys! I'm currently running into a fundamental problem and working with a lot of workarounds that I really only consider temporary solutions.
The main issue here is the collaboration between Vue.js 3 and Pinia. Pinia consists of the Store, Getters and Actions. Sometimes we have very nested objects in the Store and we only need certain parts of it. For that, it's perfect to create a getter, for example, to output only the parts of the object that I need.
But what if I want to change exactly those parts of the object from the template? My wish is that the data in the store changes as well. But since getters are readonly, this is not possible.
How would one proceed here?
Of course I would like to show you an example to underline my explanations with some practice.
I am creating a store here with a very nested object
roles
. In the gettergetElementsForCurrentRole
, which I use in the template for a v-for, I only need certain elements.To make it easier for you to understand, I'm also sending the template code here:
I loop through the
getElementsForCurrentRole
getter after using thestoreToRefs
function mentioned in the documentation to make the data reactive. My problem here is that the data is probably only partially reactive. For example, if I change the value of theSwitch
element, then the store updates successfully. This works. However, I don't seem to have access to the order of the array I'm looping. As soon as I change the order by drag and drop, I get the message:Write operation failed: computed value is readonly
.I don't understand this and I can't comprehend it.
As a workaround I currently calculate the old and the new index of the record in the array after dragging based on the event and update the store manually. But that can't be the purpose behind it. Have I fundamentally misunderstood something in the architecture? How would one approach such a case?
Beta Was this translation helpful? Give feedback.
All reactions