Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/template-expressions-restrictions.md
1---2title: Template Expressions Must Be Single Expressions3impact: MEDIUM4impactDescription: Using statements instead of expressions in templates causes compilation errors5type: capability6tags: [vue3, template, expressions, interpolation, syntax]7---89# Template Expressions Must Be Single Expressions1011**Impact: MEDIUM** - Vue templates only support single JavaScript expressions, not statements. Using variable declarations, if statements, or multiple statements will cause template compilation errors.1213Template interpolation `{{ }}` and directive bindings evaluate JavaScript expressions that produce a value. Statements like `if`, `for`, variable declarations, or multi-line code blocks are not allowed.1415## Task Checklist1617- [ ] Use only single expressions in `{{ }}` interpolation18- [ ] Use ternary operators instead of if/else statements19- [ ] Move complex logic to computed properties or methods20- [ ] Avoid variable declarations in templates21- [ ] Use `v-if`/`v-else` directives for conditional rendering2223**Incorrect:**24```vue25<template>26<!-- ERROR: Variable declaration is a statement, not expression -->27<p>{{ var greeting = 'Hello' }}</p>28<p>{{ let x = 1 }}</p>29<p>{{ const name = 'Vue' }}</p>3031<!-- ERROR: if statement not allowed -->32<p>{{ if (ok) { return message } }}</p>33<p>{{ if (user) return user.name }}</p>3435<!-- ERROR: Multiple statements not allowed -->36<p>{{ count++; return count }}</p>37<p>{{ items.push(newItem); items.length }}</p>3839<!-- ERROR: for/while loops not allowed -->40<p>{{ for (let i = 0; i < 5; i++) { } }}</p>41</template>42```4344**Correct:**45```vue46<template>47<!-- OK: Simple expressions -->48<p>{{ message }}</p>49<p>{{ count + 1 }}</p>50<p>{{ items.length }}</p>5152<!-- OK: Ternary operators for conditionals -->53<p>{{ ok ? 'YES' : 'NO' }}</p>54<p>{{ user ? user.name : 'Guest' }}</p>55<p>{{ score >= 60 ? 'Pass' : 'Fail' }}</p>5657<!-- OK: Method/function calls -->58<p>{{ formatDate(date) }}</p>59<p>{{ items.filter(i => i.active).length }}</p>6061<!-- OK: Chained expressions -->62<p>{{ message.split('').reverse().join('') }}</p>6364<!-- OK: Template literals -->65<p>{{ `Hello, ${name}!` }}</p>6667<!-- OK: Object/array expressions -->68<p>{{ { name: 'Vue', version: 3 } }}</p>69</template>7071<script setup>72import { ref, computed } from 'vue'7374const ok = ref(true)75const message = ref('Hello')76const user = ref({ name: 'Alice' })77const score = ref(85)7879// Move complex logic to computed properties80const greeting = computed(() => {81if (user.value) {82return `Welcome back, ${user.value.name}!`83}84return 'Hello, Guest!'85})8687// Or use methods for reusable logic88function formatDate(date) {89return new Date(date).toLocaleDateString()90}91</script>92```9394## Use Directives for Control Flow9596```vue97<template>98<!-- Instead of if/else in expressions, use v-if/v-else -->99<p v-if="user">Welcome, {{ user.name }}!</p>100<p v-else>Please log in</p>101102<!-- Instead of loops in expressions, use v-for -->103<ul>104<li v-for="item in items" :key="item.id">{{ item.name }}</li>105</ul>106107<!-- Conditional display without removing from DOM -->108<p v-show="isVisible">This toggles visibility</p>109</template>110```111112## Reference113- [Vue.js Template Syntax - Using JavaScript Expressions](https://vuejs.org/guide/essentials/template-syntax.html#using-javascript-expressions)114- [Vue.js Conditional Rendering](https://vuejs.org/guide/essentials/conditional.html)115