# vue-facing-decorator — Class-Field Handler Traps
<!--
ref: @protocol/vue-facing-decorator-traps
tags: [vue, vue-facing-decorator, typescript, refs, event-listeners, gotcha]
triggers: [template ref is undefined, removeEventListener not working in Vue, arrow class field handler, this is wrong in a Vue class component]
trigger_prompt: When a class-based Vue component's handler cannot see template refs, or an event listener will not detach.
category: reference
version: 10
updated_at: 2026-11-01T02:55:32.622Z
-->
Arrow class fields capture `this` at construction, before the reactive proxy exists — so they cannot read template refs. Bind handlers inside `mounted()` and store them on instance props.
Each decision below is a section — fetch just one with `?section=<slugified-heading>` rather than pulling the whole branch.
---
## The trap
**With vue-facing-decorator, a class-field arrow handler (`private onScroll = () => this.someRef.foo`) captures `this` at construction — but template refs live on the reactive proxy and are undefined on that captured instance. Bind handlers inside mounted() instead.**
Discovered in `frontend/src/components/landing/SideBySideAnatomy.vue` while
wiring sync-scroll: the listener fired, but `this.rawBody` inside it was
`undefined`. Refactoring the handlers from arrow class-fields to closures
created inside `mounted()` (stored on instance properties so they can be
removed in `beforeUnmount`) fixed it instantly.
**Why:** vue-facing-decorator wraps the class instance in Vue's reactive
proxy. `@Ref()` populates the refs on the proxy. A class-field arrow
function (`private onX = () => {...}`) captures `this` at construction
time — that `this` is the raw instance, not the proxy. So `this.rawBody`
inside the arrow stays undefined even after the template ref binds.
**How to apply:**
- For event handlers that need to read `@Ref()` template refs or
`@Prop()` props at call time, define the handler **inside** `mounted()`
as a regular closure and store it on a class property so
`beforeUnmount()` can `removeEventListener` it.
- Same constraint likely applies to any class-field arrow that touches
reactive state (`@Watch`-driven properties, computed-backed values).
- Regular methods (no `=` arrow assignment) are fine — they're invoked via
the proxy at call time, so `this` is the proxy.
## `render` is a reserved name — a method called that is never callable
**A `render()` method on a vue-facing-decorator class component is hoisted out of `methods` and installed as the component's render function, so `this.render()` throws `TypeError: this.render is not a function`.**
Found in `frontend/src/components/EventMap.vue`. A Mapbox
component held its "push the current points into the map" logic in
`private render()`, called from the map's `load` handler and from a
`@Watch('result')`. Both call sites threw on every load.
**Why it is worse than a normal crash:** the throw lands *after* the expensive
setup. The Mapbox map was constructed, the basemap tiles drew, the navigation
control worked, panning worked, and the count rendered above the canvas said
"1040 on the map". The only missing thing was the 1040 markers. A component that
half-works looks like a data problem — the map was assumed to be plotting a
corpus with no coordinates in it, and the bug survived a review and a deploy.
**How to apply:**
- Never name a class-component method `render`. Use `draw`, `paint`, `sync`,
`apply` — anything. The same care applies to any Vue component option that is
also a plausible method name: `data`, `setup`, `emits`, `props`, `expose`.
- TypeScript will not warn. `this.render()` type-checks against the class
declaration; the hoisting happens at runtime in the decorator.
- Symptom to recognise: an imperative library (Mapbox, Chart.js, a canvas) whose
container initialises correctly but never receives data, with
`this.<name> is not a function` in the console.
- Cheapest detection is a page-load error probe in CI —
`page.on('pageerror')` over each route — because this class of failure never
produces a blank screen to notice.
---
## A bare boolean attribute is `""`, and `""` is falsy
**Vue coerces a valueless attribute (`<Editor row-mode />`) to `true` only when the prop declares `type: Boolean`. Without it the prop arrives as the empty string, which is FALSY — so the template plainly says the flag is set and every read of it says it is not.**
Found in `frontend/src/components/entry/EntryRowCellEditor.vue`. `@Prop({ default: false }) rowMode!: boolean` looks
complete and type-checks: the TypeScript annotation says `boolean`, and
`vue-facing-decorator` does not turn that annotation into a runtime `type`
unless `reflect-metadata` is emitting design types. The decorator options are
the only thing Vue sees.
**Why it is worse than a normal wrong value:** the flag was the branch between
two whole interaction modes. In row mode the editors must hand Tab back to the
browser and ignore blur; in cell mode blur commits. Reading the flag as false
meant every field of a row edit ran in cell mode, so the first click from one
field to the next blurred the previous one, committed the row, and closed the
edit — which looks exactly like a row edit that saves eagerly, not like a prop
that never arrived.
**Seen again** in `frontend/src/components/entry/EntryMetaBar.vue`: the entry header teleports a meta bar into the breadcrumb row and passes `compact` bare to switch it from a full-width `justify-between` row to a packed, truncating one. Compact mode had **never once engaged** in production. Nothing threw and every control still rendered — you simply got the *other* valid layout, which reads as a CSS or taste problem, so that is where the debugging time went. The component's own doc comment described the compact behaviour in detail and the behaviour was not happening; the prose was trusted over the DOM for two months. Two independent instances in one codebase is the signal: this is not a slip, it is the default behaviour of the decorator.
**How to apply:**
- Declare `type: Boolean` on every boolean prop, or always pass it bound
(`:row-mode="true"`). The bare-attribute form is the one people write.
- TypeScript will not warn, and neither will Vue: `""` is a legal value for a
prop with no declared type.
- Symptom to recognise: a component behaves as though a flag you can see in the
template is off. `console.log(typeof this.flag)` printing `string` is the
whole diagnosis.
- Assert it in the DOM, not in the source: `el.classList.contains('is-compact')`
in a browser check. A prop silently taking a falsy value is invisible to
typecheck, to unit tests that mount with explicit bound props, and to reading
the component — all three agree with the template, and the template is wrong.
## Closing a focused editor fires `blur` on the way out
**Unmounting the element that has focus fires a `blur` event first. On an editor whose blur handler saves, that means Escape STORES the draft it was meant to discard, and any commit that closes the editor sends its write twice.**
Found in the same component and the same session. The editor followed a shape
that had already shipped and looked correct: Escape emits `cancel`, the parent
clears the open-cell address, the editor unmounts. Blur-saves is a deliberate,
good behaviour on the same control — clicking from one cell to the next should
commit. The two are only in conflict during teardown.
Three symptoms, all from one cause:
- Escape wrote the value it was supposed to throw away.
- Enter sent the same PATCH twice.
- Enter's "commit and move to the next cell" undid itself: the unmount-blur ran
a second commit with no move, which reset the address the first one had just
set. The caret stayed put and the feature read as unimplemented.
**How to apply:**
- Give the editor a one-shot guard — a private `finished` flag set by the first
`commit` or `cancel`, checked by all three of `commit`, `cancel` and `blur`.
- Put the guard in the CONTROL, not in the parent. It is the control's own
lifecycle that produces the second event, and a parent-side dedupe would have
to be re-derived by whoever adds the next editor.
- This is not framework-specific. Any blur-saves editor that is removed from the
DOM to close has it.
- It survives unit tests completely: nothing unmounts a focused input except a
real browser. A DOM-level pass (Playwright over a mounted component) is what
finds it, and it finds it on the first assertion.
## A leading HTML comment makes a component a fragment; Vue also condenses space runs
Measured 2026-10-23 to 10-26. (1) An HTML comment before the root element of a template makes the component a fragment: `$el` is undefined, template-ref canvases stay at the default 300x150 and paint nothing, and typecheck is clean. A root-level `v-if` plus `nextTick` has the same shape (the element arrives after setup); make the element unconditional. (2) Vue condenses runs of spaces in a text node, so a separator made of spaces disappears; build the gap with CSS padding. (3) A per-element 3D CSS transform breaks `mix-blend-mode`, so overprinted layers go opaque in motion; draw the tilt in canvas. Verify canvas work in a real browser, not by typecheck.
## `<Teleport>` into a `v-if`'d target fails silently: use `<Teleport defer>`
Measured 2026-10-30: three `<Teleport>` blocks targeted elements inside a container that was itself under `v-if`. On first render the target did not exist yet, the children mounted into a detached subtree, and Vue showed an empty region with no error or warning. Fix: add `defer` to each Teleport so target resolution waits until the target is mounted, then check the region is not empty in a real browser. Related: layout keyed on viewport media queries is wrong when the component lives in a narrow pane inside a wide window; pass an explicit width-mode prop instead of relying on the viewport.
---
*Related: `@protocol/python-js-word-boundary-trap` · `@protocol/naiveui-theme-override-parsing-trap` · `@protocol/postgres-skip-locked-limit-trap` · `@protocol/postgres-timestamp-self-match` · `@protocol/zsh-tied-variable-trap`*
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*