Inconsistent ref handling between dev and build
Version
3.2.4
Reproduction link
Steps to reproduce
Inside script-setup, use a ref
attribute in template with a non-existant backing field.
To make use of that ref, you can use $refs
in an event handler.
The linked repro uses <input ref="input">
and @click="$refs.input.focus()"
.
What is expected?
Probably should work. At least should be consistent in dev vs build mode. Ideally consistent with script-classic, too.
What is actually happening?
It works in almost all situations:
- Always works with classic script;
- Works with script-setup in dev.
Fails in script-setup when deployed with build.
The reason it fails is because the script-setup codegen is different between dev and build.
In repro, you can see that ref="input"
was simply translated to line 20: ref: input
.
Except input
is a free variable that is not declared.
All other cases have a different codegen that don't require a global variable (and the ref is usable from $refs
).
It does not seem to cover other scenes, there are still some problems like: link with other bug.