Frontend Development 7 min read

Understanding and Fixing the Reset Behavior Issue in Element‑Plus Form Component

This article analyzes why the reset button in an Element‑Plus form clears all fields instead of restoring the initial values, explains the underlying cause related to the onMounted lifecycle timing, and presents several practical solutions—including lifecycle adjustment, patch‑package, component extension, and repository forking—to reliably preserve initial form data in Vue projects.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Understanding and Fixing the Reset Behavior Issue in Element‑Plus Form Component

Problem Description and Reproduction

A developer added an onMounted hook to set initial values for a form field after fetching data, but when the reset button is clicked, the email field is cleared instead of being restored.

Problem Analysis

The reset logic in Element‑Plus calls resetFields , which iterates over form items and invokes resetField . Inside resetField , the current value is overwritten with initialValue . The initialValue variable is set during the component's onMounted hook.

Because the Element‑Plus form component mounts before the user’s component, the onMounted in the user component runs later, so the value assigned there is not recognized as the form’s initial value.

Thoughts

Moving the value assignment to onBeforeMount ensures it runs before the form component records its initial value. However, when the data is fetched asynchronously, simply awaiting the fetch does not guarantee the timing.

How to Modify the Source

Several approaches are proposed:

Patch‑Package : Create a patch for the Element‑Plus source to adjust the initialization logic, but this ties the fix to a specific version.

Vue‑Specific Extension : Extend the original FormItem component, delete its mounted hook, and re‑implement it with custom logic (e.g., listening to localStorage changes) to set initialValue correctly. delete FormItem.mounted const FormItemPatched = { extends: FormItem, mounted() { if (props.prop) { formContext?.addField(context) initialValue = clone(fieldValue.value) window.onstorage = payload => { initialValue = payload } } } }

Fork and Maintain : Fork the Element‑Plus repository, apply the fix directly, and set up a workflow to monitor upstream releases and merge updates as needed.

Summary

The article demonstrates a bug in Element‑Plus form reset behavior, explains why it occurs, and outlines three practical ways to fix it: adjusting the lifecycle order, using patch-package , extending the component, or maintaining a forked version, with the Vue‑specific extension being the most elegant solution for typical projects.

frontendVuebugPatchformElement-Plusreset
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.