Moving between two converter URLs does not necessarily mean Vue creates a fresh component instance. I'll use a feet-to-meters page to show how to reset reused input refs without letting two-way watchers overwrite the intended defaults.
I used a feet-to-meters page as a concrete example. The arithmetic is simply multiplication by 0.3048, but the implementation is more interesting as a small case study in route-driven data, editable state, and resetting both sides of a two-way calculation.
Put Page Identity in Route Data
The application describes fixed conversion pairs as data. Each pair carries a slug, category, source unit, destination unit, development-order value, and localized content.
The router expands those definitions into routes that use one shared component. It places the pair slug in route metadata, so the component can look up the current definition:
const pair = computed(() => getPairBySlug(route.meta.slug));
The lookup function is deliberately small:
export function getPairBySlug(slug) {
return allConverterPairs.find((p) => p.slug === slug);
}
For the feet-to-meters page, the selected definition uses ft and m. Another route can select a different pair without requiring a second copy of the input template or synchronization functions.
The important distinction is between page identity and component identity. The URL and metadata identify a different conversion, while Vue may still be rendering the same component type. The app renders the routed component without a URL-based key, so navigation between these pair pages can reuse its instance. A setup-time read copied into a plain constant would not express that changing relationship.
Using a computed lookup means the displayed pair can follow the reactive route metadata. It does not, by itself, reset every editable ref owned by the component. That second responsibility needs its own code.
Reset Both Fields Before Recomputing
The component begins with a left value of 1, a right value of zero, and a synchronization guard. It watches the pair slug:
watch(
() => route.meta.slug,
() => {
isSyncing = true;
fromValue.value = 1;
toValue.value = 0;
isSyncing = false;
syncFromLeft();
},
);
This defines a clear navigation policy: moving to another pair starts that pair with a source value of 1. It does not carry an arbitrary measurement from the previous page into a new set of units.
Why assign both fields while the guard is active? Both refs have synchronous watchers. Without the outer guard, assigning 1 to the source could immediately update the destination. The next reset assignment, setting the destination to zero, would then trigger a reverse conversion and overwrite the source with zero. The final left-to-right synchronization would convert that zero, not the intended 1.
The guarded reset writes the defaults without treating either write as a new user edit. After releasing the guard, it deliberately performs one left-to-right synchronization using the current pair.
That final call is important. Merely resetting the destination to zero would leave the UI showing a placeholder rather than a valid conversion. On the feet-to-meters route, one foot should produce 0.3048 meters.
This is a product choice as well as a reactive detail. Another application might preserve the last entered physical length across navigation, but that would require defining which unit gives that saved magnitude meaning. Preserving an unlabeled number is not the same as preserving a measurement.
Keep User Edits Separate from Derived Writes
The shared left-side synchronization function reads the current pair at the moment it calculates:
function syncFromLeft() {
if (isSyncing || !pair.value) return;
isSyncing = true;
toValue.value = categoryConfig.value.round(
categoryConfig.value.convert(fromValue.value, pair.value.from, pair.value.to),
);
isSyncing = false;
}
The reverse function swaps the unit keys and writes the source field. Both value watchers use synchronous flushing:
watch(fromValue, syncFromLeft, { flush: "sync" });
watch(toValue, syncFromRight, { flush: "sync" });
That means the guard is still active when a programmatic write invokes the opposite watcher. The boolean depends on this timing: changing these watchers to deferred execution would require revisiting the guard, because it would already be false when queued callbacks ran.
That protects directionality. If the user types six feet, the left value is authoritative and the right side becomes 1.8288 meters. If the user instead edits the meters field, that value becomes authoritative for the reverse calculation.
The category configuration selects the correct conversion and rounding functions. For length pairs, those functions use the shared meter-based scales and round output to at most six decimal places. The component does not need a feet-to-meters-specific watcher.
An initial syncFromLeft() also runs during setup. The first server-rendered result can therefore contain the correct conversion instead of waiting for a browser mount hook to replace zero. That initialization serves a different purpose from the route-change watcher: one establishes the first result, and the other handles later pair changes.
I find this separation easier to test than relying on a forced component remount for every navigation. The reset policy is visible in the component, and the same arithmetic path serves initialization, navigation, and ordinary edits.
Define What Should Survive Navigation
The watcher observes route.meta.slug, not every route change. An in-app language change that keeps the same pair slug is not, by this watcher alone, an instruction to reset the measurement. The localized content is computed separately from the pair and current locale. This does not promise persistence across a full page load: a new component instance starts from its setup defaults.
There are limits to the data lookup too. getPairBySlug returns undefined when no entry matches. Some synchronization code guards against a missing pair, but other setup expressions assume registered route data exists. The component is designed for the router's generated definitions, not as a complete arbitrary-slug error page.
The six-decimal rounding policy also remains in effect after navigation. For example, 0.000001 feet converts to 0.0000003048 meters, which rounds to zero here. The factor 0.3048 is exact by definition, but JavaScript floating-point arithmetic and output rounding still impose limits. A reset does not improve numerical precision, and retaining an edited value would not restore digits already rounded away.
I checked the component's conversion block with actual Vue refs and watchers, using reactive route metadata and a synthetic reverse-pair fixture. Entering 2 meters kept that edited field at 2 and produced 6.56168 feet. After changing the slug and awaiting nextTick(), the source reset to 1; returning to feet-to-meters produced 0.3048 meters. Replacing the metadata with the same slug preserved the edited values. Removing only the reset guard made a pair change end at zero on both sides.
Those are component-state checks, not browser navigation or localization tests. I'd still test the rendered labels and the actual language-switching flow separately; keeping a slug unchanged only establishes that this particular watcher should not reset the fields.
I turned this into a small free tool: Feet to Meters Converter.
Top comments (0)