You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Vue templates cannot declare new local reactive variables (scopes).
All data accessible to the template must be declared in setup() and then returned to the render context. With a small exception of v-for that allows to declare variables within the template
This sometimes leads to repetition and less expressive templates.
Here, doubled and now are reactive variables, visible only inside this part of the template.
They automatically update if their dependencies (count, etc.) change.
Benefits of the proposed feature
Improved readability: Avoids repeating complex expressions in multiple template locations.
Locality: Keeps derived values close to where they’re used, instead of polluting setup().
Self-documenting templates: Clear naming for computed values improves maintainability.
Reactive by design: Automatically tracks dependencies and updates on change.
Backward compatible: Adds no breaking changes, works alongside existing template syntax.
Considerations / Implementation notes
The compiler/runtime would need to treat each property in the v-data object as a computed() expression.
The scope should be reactive and automatically disposed when the block is unmounted.
It must properly merge into the template’s reactive context without leaking variables outside its scope.
Ideally, it should work both on real elements and blocks.
Should be compatible with SSR, hydration, and existing optimization passes (hoisting, static trees, etc.).
Should be not readonly
Summary
This would enhance Vue’s declarative power and improve developer experience by allowing localized computations directly in the template without additional boilerplate or repetition.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What problem does this feature solve?
Currently, Vue templates cannot declare new local reactive variables (scopes).
All data accessible to the template must be declared in
setup()
and then returned to the render context. With a small exception ofv-for
that allows to declare variables within the templateThis sometimes leads to repetition and less expressive templates.
For example:
If the same derived expression (
count * 2
) is used multiple times, developers must either:There’s currently no way to define a local, reactive variable directly within the template itself without small hacks.
For example:
What does the proposed API look like?
Here,
doubled
andnow
are reactive variables, visible only inside this part of the template.They automatically update if their dependencies (count, etc.) change.
Benefits of the proposed feature
Improved readability: Avoids repeating complex expressions in multiple template locations.
Locality: Keeps derived values close to where they’re used, instead of polluting setup().
Self-documenting templates: Clear naming for computed values improves maintainability.
Reactive by design: Automatically tracks dependencies and updates on change.
Backward compatible: Adds no breaking changes, works alongside existing template syntax.
Considerations / Implementation notes
The compiler/runtime would need to treat each property in the v-data object as a computed() expression.
The scope should be reactive and automatically disposed when the block is unmounted.
It must properly merge into the template’s reactive context without leaking variables outside its scope.
Ideally, it should work both on real elements and blocks.
Should be compatible with SSR, hydration, and existing optimization passes (hoisting, static trees, etc.).
Should be not readonly
Summary
This would enhance Vue’s declarative power and improve developer experience by allowing localized computations directly in the template without additional boilerplate or repetition.
Beta Was this translation helpful? Give feedback.
All reactions