How can I achieve a time travel feature using Vuex ? I want to go back for a previous state, to undo something.
Is that possible out of the box ?
Any idea on how to achieve that ?
was hoping for something like store.rollback(1)
The best approach is not to keep the record of the state snapshots, but rather, keep the record of committed mutations.
To UNDO one step:
This approach is easier on the memory (esp if you have a large store), and is more in line with how the store should be treated.
A very good breakdown here:
https://vuejsdevelopers.com/2017/11/13/vue-js-vuex-undo-redo/
A working solution here:
Just implement it by your own, add a prevState to your store, you can only select the parts that you want to make it undo-able.
Here is the simplest example, which only support 1 history record:
store
mutations:
If you need to have more history, just put them in an array
and then you can use
state.countHistory.pop()
andstate.countHistory.push(xx)
to undo/save recordsAnother solution is plugin (middleware), in case you want to save all the history automatically.