The code below for a vuejs component successfully sets the initial state of the data for playerName, time, and errors. Extracting the initial data to a function allows me to easily reset the data back to the initial state.
Great...But... I only want time and error to get reset to the initial state and I want playerName to continue to persist.
Is there a recommended way to accomplish this? I can only seem to find all or nothing approaches or a clunky approach to reset manually that will require me to remember if I change in the future.
Appreciate you looking and any assistance.
function initialState (){
return {
playerName: '',
time: 0,
errors: 0
}
}
//In my component:
data: function (){
return initialState();
}
//Call this method when I want to start over
methods:{
reset: function (){
Object.assign(this.$data, initialState());
}
}