Skip to content

v-el and v-ref usage change after binding syntax update #1292

@yyx990803

Description

@yyx990803

This was originally raised in https://github.com/yyx990803/vue/issues/1173#issuecomment-139092569

Problem

v-ref and v-el are some special built-in feature that allow you to register a child component or a DOM node on the parent component. They are not really directives, but were presented as "literal directives" in previous versions. In 1.0.0, we are making literal directives more explicit, which also makes writing them a little more verbose (example: v-ref.literal="xxx").

In addition, the user needs to consult the doc to remember that v-el registers under vm.$$, and v-ref registers under vm.$. The $ and $$ are not very explicit and does not map well with the directive names.

Proposal

Rename to more explicit properties:

  • vm.$ -> vm.$refs
  • vm.$$ -> vm.$els

Use the arguments in the new binding syntax to mark the ref id:

<!-- child component ref, registers vm.$refs.child -->
<comp v-ref:child></comp>
<!-- elsewhere... -->
{{$refs.child.msg}}

<!-- element ref, registers vm.$els.node -->
<div v-el:node></div>

Caveats

camelCase names are converted to all lowercase when the HTML is parsed:

<comp v-ref:someThing></comp>

Gets rendered as:

<comp v-ref:something></comp>

Thus, it is necessary to use the dash-case <-> camelCase mapping for refs too (similar to props):

<comp v-ref:some-thing></comp>
{{ $refs.someThing.msg }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions