zehuichan

vue-pitfalls

'Verified pitfalls in Vue 3 business pages, each checked against library source or reproduced: keep-alive name matching, table selection cleared when data is replaced, echo hook timing, close/cancel double-binding that depends on the wrapper''s emits, close firing after a successful save, cleared controls emitting different empty values, date picker writing a Date; UI-library specifics (Element Plus verified; antdv-next and Vant mapped) in references. Use when debugging forms, modals, cached tabs, tables with selection, or when reviewing such code.'

zehuichan 0 Updated 2d ago

Resources

2
GitHub

Install

npx skillscat add zehuichan/zstack/vue-pitfalls

Install via the SkillsCat registry.

SKILL.md

Pitfalls

Every entry here was verified by reading source or reproducing the failure, and says which. An entry that is still a hypothesis goes in the library reference under "Unverified", not here. Each entry is symptom, cause, fix.

Find the platform and library from package.json (element-plus, antdv-next, ant-design-vue, vant, uni-app, @tarojs/*) and read the matching file in references/. For a library with no file there, read its source before asserting anything, then add the file.

Cache name differs from the route name

  • Symptom. A tab that should keep its filters and scroll position starts fresh on every return, and nothing reports an error.
  • Cause. KeepAlive skips a component unless include matches its name: !name || !matches(include, name) leaves it uncached. The name is Component.name || Component.__name. A string include is split on , and compared exactly, so case and slashes count; an array and a regular expression are also accepted. For <script setup> the SFC compiler fills __name from the file's base name, so a page with no explicit name is called index or action. Using the open tabs' route names as the include list is the application's tab store, not Vue.
  • Fix. defineOptions({ name }) equals the route name; an explicit name wins over __name. A backend-delivered menu fixes the route name's shape, so read the menu's value rather than guess. Forms seen in one project: PascalCase, a path such as /finance/day, and a local entity/page.
  • Source. Read in @vue/runtime-core 3.5.30 (KeepAliveImpl, matches, getComponentName) and @vue/compiler-sfc 3.5.30 (__name from the filename). The route-name forms come from one project's router.

Table selection is cleared when the data array is replaced

  • Symptom. Rows the user ticked disappear after a query, a page change, or any refresh that assigns a new array, and the selection-change handler fires with an empty list. Batch buttons that depend on the selection go disabled.
  • Cause. In Element Plus the table's setData takes one of three paths. With reserve-selection on the selection column it only asserts that row-key is set and leaves the selection alone. Otherwise, if the array is a different object from the last one, it clears the selection and emits the change event. If it is the same array, it removes only the selected rows no longer present.
  • Fix. Decide the intent. Selection that should not outlive a refresh needs nothing, but a handler must not assume ticked rows survive an await query(). Selection that must span pages needs reserve-selection on the selection column and row-key on the table; without row-key the table throws [ElTable] prop row-key is required. Expect selected rows from other pages in the selection list then.
  • Source. Read in es/components/table/src/store/index.mjs (setData) and store/watcher.mjs (clearSelection, cleanSelection, assertRowKey), Element Plus 2.14.0. Other libraries: not checked.

Echo on the after-open event

  • Symptom. A modal's form is empty or stale on a later open, or the echo runs before the form's ref exists.
  • Cause. With content destroyed on close, the after-open event fires on every open, once the content is mounted. A watch on the visibility flag fires when the flag flips, which is earlier: in Element Plus the dialog renders its body in its own watcher after the flag changes and destroys it again on close.
  • Fix. Bind the echo to the after-open event (names in Adapters) and enable destroy-on-close.
  • Source. Read in es/components/dialog/src/use-dialog.mjs (2.14.0) and in the project's modal wrapper, which forwards @opened through $attrs and sets destroy-on-close. Other libraries: not checked.

Close and cancel bound together run the handler twice — but only when the wrapper does not declare close

  • Symptom. A cancel handler runs twice for one close; anything inside it that prompts or calls out happens twice.
  • Cause. The wrapper forwards unknown listeners through $attrs, so whether a parent's @close reaches the inner component depends on one thing: is close in the wrapper's emits? If it is not, the listener falls through and the wrapper's own close-mapped event fires too, so two listeners reach the handler for one close.
  • Fix. Read the wrapper's emits before deciding. The two wrappers in the source project differ:
    • Modal — emits: ['cancel', 'confirm'], no close. The parent's @close falls through to el-dialog's own close, while @cancel is the wrapper's footer event. Binding both to one handler is safe, and 42 of 59 popup files do it.
    • Drawer — emits: ['update:modelValue', 'cancel', 'confirm'], and the inner el-drawer's @close calls cancel. Binding both runs the handler twice; 1 of 59 popup files does it.
  • Source. Read in the project's Modal and Drawer wrappers, and counted across src/views: 43 files bind close and cancel to the same handler, 42 of them on a modal and 1 on a drawer. An earlier version of this entry claimed the double binding was universally wrong; the count disproved it.

A close handler also runs after a successful confirm

  • Symptom. A handler bound to the dialog's close event runs after a save that closed the dialog itself, and does cancel-style work: it resets the form, prompts "discard changes?", or reports a cancellation.
  • Cause. In Element Plus the close event is emitted when the leave transition starts (beforeLeave), whatever started the close: the X, Esc, a footer button, or the component setting its own modelValue to false. It is not limited to user dismissal.
  • Fix. Keep a close-bound handler idempotent and free of prompts or requests. Anything that must happen only on a user cancel goes on the cancel button's own handler.
  • Source. Read in es/components/dialog/src/use-dialog.mjs (2.14.0). Other libraries: not checked.

A cleared control reports an empty value, and which one depends on the control

  • Symptom. A filter that defaults to null ends up as undefined or '' after the user clears it, so === null checks and "is this filled in" logic disagree. Two filters on the same page can disagree with each other.
  • Cause. A clearable control calls back with its own empty value, and the controls in one library do not agree. In Element Plus 2.14.0 a cleared el-input writes '', a single el-select writes undefined ([] when multiple), and a cleared date or time picker and an el-input-number write null. value-on-clear, a global config, or a wrapper can change each of these.
  • Fix. Treat '', undefined, and null as empty: test for emptiness, do not compare to null. If the request layer filters empty values before sending, a cleared field does not reach the backend as a value; confirm that it does before relying on it.
  • Source. Read in Element Plus 2.14.0: input.vue clear, select/src/useSelect.mjs (useEmptyValues(props)), time-picker/src/common/picker (useEmptyValues(props, null)), input-number (valueOnClear default null), and hooks/use-empty-values.

A date picker without value-format writes a Date

  • Symptom. A date chosen in the form reaches the backend one day early, or as a UTC timestamp such as 2025-12-31T16:00:00.000Z when the user picked 1 January in UTC+8.
  • Cause. Without value-format the picker emits the Date object itself; its formatter returns the value unchanged when no format is set. JSON.stringify turns a Date into a UTC ISO string.
  • Fix. Set value-format (for example YYYY-MM-DD) on every date and date-time picker whose value is sent as is. In a wrapper, put it in the wrapper's defaults rather than on each page.
  • Source. Read in es/components/time-picker/src/utils.mjs (formatter) and composables/use-common-picker.mjs, Element Plus 2.14.0. The serialization was reproduced in Node with TZ=Asia/Shanghai: JSON.stringify({ d: new Date(2026, 0, 1) }) gives 2025-12-31T16:00:00.000Z. Other libraries: not checked.

Adapters

Role PC · Element Plus PC · antdv-next H5 · Vant
After-open (echo) event @opened; destroy-on-close makes it fire on every open @afterOpenChange(open) on Modal, act when open is true; destroyOnHidden unmounts on close; Drawer no longer declares afterOpenChange, see the reference popup @opened
Close event @close; fires whenever the dialog starts closing, whatever the cause not checked not checked
Form reset resetFields(); whole-model replacement has a timing rule, see the reference resetFields(); store-driven form, unverified, see the reference resetValidation() clears validation state only; reset values by replacing the model
Value of a cleared control '' input, undefined select, null date picker and input-number; see the reference not checked not checked
Selection after the data array is replaced cleared, unless reserve-selection and row-key not checked not checked
Date picker value Date object; value-format makes it a string not checked not checked

Mini-program: not verified; add a column when a real project exists.

References: Element Plus, antdv-next, Vant.