T
T
Timofey Mikhailov2020-04-27 00:15:14
Vue.js
Timofey Mikhailov, 2020-04-27 00:15:14

How to properly copy data from Vuex?

The thing is, I need to get data from $store (vuex) into the component , work with it, and in which case save it back to vuex .
But the "situation" is (as I understand it) that the Object.assign method copies all reactivity to another array .. Well, or how to explain it, I don't know ... =(
Here is my method:

getData(){
      this.allBooks = Object.assign({}, this.$store.getters.getAllBooks())
    }

Okay, next change the title of the first book
this.allBooks[0].title = 'test name'
      console.log(this.$store.getters.getAllBooks())


I look at the console, and the title of the book has changed ... Tell me what I'm doing wrong ...

ps
Yes, I completely forgot to explain what all this is for.
I need to somehow check whether the title of the books has changed,
and since the titles of the books are automatically changed in my component and in the $store, I can’t find out which of the books have been renamed.
Here.

Possible solution
So, this morning, I paid attention to " Modules ".
those. if I put the book in a Module, then I can make changes there, and they will not be dynamically adjusted in the global array.
This seems to solve my problem.
But I'm not sure if it's okay to fence the whole module in such a situation. Or you can get by with small sacrifices. But, unfortunately, I can’t find any other solution =(

trouble ...
in fact, the creation of the module did not help me. More precisely, I scattered all the functions in different folders and files, and everything began to look pretty "nice". But my problem that didn't solve it, and that's when I gave up completely

SOLUTION
Oh, yes, it's StackOverflow

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VegasChickiChicki, 2020-04-27
@VegasChickiChicki

Based on the principles of Vuex.

get data from $store (vuex), work with it,
- getters
and in which case save back to vuex.
- mutations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question