Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Template Local Variable Binding Proposal #12323

CorruptionTiger started this conversation in Ideas
Discussion options

Summary

Propose a new template syntax for creating local variable bindings with reactive references, similar to Vue's v-slot but more straightforward for simple variable aliasing.

Motivation

When working with deeply nested reactive objects or when we want to create local aliases for reactive data, current solutions either require script setup variables or slot syntax. A more direct syntax in template could improve code readability and reduce boilerplate.

Proposed Syntax

<template v-def="sourceData => localName">
 <!-- localName is now a reactive reference to sourceData -->
</template>

Example Usage

<script setup>
import { ref } from 'vue'
const data = ref({user: {name: 'Tom', age: 18}})
</script>
<template>
 <template v-def="[data.user,userInfo]">
 <input v-model="userInfo.name" />
 <input type="number" v-model="userInfo.age" />
 </template>
</template>

Benefits

  1. More intuitive syntax for creating local variable bindings
  2. Maintains reactivity
  3. Supports two-way binding
  4. Reduces the need for script setup variables when only template scoping is needed
  5. Similar to JavaScript arrow function syntax, making it familiar to developers

Current Alternatives

Currently, developers need to either:

  1. Create computed properties in script setup
  2. Use v-slot syntax
  3. Use toRefs in script setup

Questions for Discussion

  1. Is this syntax intuitive enough?
  2. Should this be a built-in feature or remain as a custom directive?
  3. Are there potential edge cases we should consider?
  4. How would this interact with TypeScript type inference?

Looking forward to the community's thoughts and feedback on this proposal.

You must be logged in to vote

Replies: 2 comments

Comment options

see #7218

You must be logged in to vote
0 replies
Comment options

Currently, Vue templates do not provide a clean built-in way to create a local alias for a value inside a template block.
As a result, I discovered this unusual workaround :

<template v-for="i in [var]">
 {{ i }}
</template>

This takes advantage of v-for’s iteration aliasing, using a single-element array only to introduce a temporary variable i.
While it works, this pattern is semantically incorrect, reduces readability, and introduces unnecessary cognitive burden for teams.

I would like to propose adding a dedicated directive to provide clean, explicit template-level aliasing.

Vue SFC Playground

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /