-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to reset a component? #702
Comments
That's an interesting problem. Since you know that all a component's state is stored in the $data variable, you can always just override it with the original data. And you can store that original data (or rather a function that generates it) in the same module as the component like this: //This function is exactly the same as your data function for the component originally was
function getDefaultData() {
return {
a: "A",
b: "B"
}
}
module.exports = {
data: getDefaultData,
methods: {
resetData: function () {
this.$data = getDefaultData();
}
}
}; This is assuming that your components are CommonJS modules, which I recommend doing anyway with Browserify and the Vueify transform. |
This seems like it should work, thanks! |
@TMiguelT @cheapsteak it doesn't work anymore in Vue 2.0 |
module.exports = { I write it like above, it can't work. it must be a function to get the default data when you wanna reset. why? |
You can do the above in Vue 2.0 with |
it works, thx :) |
You can do this: |
Object.assign(this.$data, this.$options.data()) it works |
Caution,
|
@thollar13 : thank you for the solution. How would that work in a component where |
@wsw70 |
What about resetting to initial property values? And, running all lifecycle hooks? |
const dataDefaults = {
_object: {
a: false,
b: ''
},
}; data() {
return {
_object: dataDefaults._object,
}
}, reset() {
Object.assign(this.$data, this.$options.data())
} |
@bradley-varol Because you're initially setting |
For anyone stumbling upon this via Google, I'd warn against using |
Best way, changing component key: http://michaelnthiessen.com/force-re-render/ |
As of February 2019, there's excellent package that handles default data and resetting components using a mixin with ease: https://github.com/ianwalter/vue-component-reset#readme |
This works for the data parent, but doesn't update the data passed to the child component. |
Adding the key to the component did the trick for me. |
This is a very good point to keep in mind while resetting like this because I use variables in data as key:value mapping for function calls for various scenarios:
When you have variables like this, these do not reset automatically on just Doing this always gave me 'undefined' values. So, make sure you apply the context as mentioned by @ifnot , This will once again bind the component's methods to the data variables. |
Is this solution actual for Vue 3.X? |
Currently if there's a component I want to reinitialize, I have to include the component as an expression, set that expression to blank, then reset the expression back to the component
Is there a better way to do this?
The text was updated successfully, but these errors were encountered: