Subscribe on changes!

Inconsistent ref handling between dev and build

avatar
Aug 24th 2021

Version

3.2.4

Reproduction link

https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgbXNnID0gcmVmKCdIZWxsbyBXb3JsZCEnKVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPGgxPnt7IG1zZyB9fTwvaDE+XG4gIDxpbnB1dCB2LW1vZGVsPVwibXNnXCIgcmVmPVwiaW5wdXRcIiAvPlxuICA8YnV0dG9uIEBjbGljaz1cIiRyZWZzLmlucHV0LmZvY3VzKClcIj5cbiAgICBGb2N1cyBpbnB1dFxuICA8L2J1dHRvbj5cbjwvdGVtcGxhdGU+In0=

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).

avatar
Aug 27th 2021

em.. how can i fix this..

avatar
Aug 27th 2021

@heeronchang as a workaround

<input v-model="msg" ref="'input'" />
avatar
Sep 2nd 2021

It does not seem to cover other scenes, there are still some problems like: link with other bug.