1 Minuten Lesezeit
Vue Tip: Simple Expressions in Templates
Vue component templates should only include simple expressions, with more complex expressions refactored into computed properties or methods.
KI-Assistent fragenMichael Hoffmann
@mokkapps

Your Vue component templates should only include simple expressions as they make your template more declarative.
If you have more complex expressions in your Vue.js component templates, you should refactor them into computed properties or methods. This allows us to reuse the code.
<template>
<span>{{
fullName
.split(' ')
.map((word) => {
return word[0].toUpperCase() + word.slice(1)
})
.join(' ')
}}</span>
</template>
<template>
{{ normalizedFullName }}
</template>
We have moved the complex expression to a computed property:
<script setup>
const props = defineProps({ fullName: String })
const normalizedFullName = computed(() =>
props.fullName
.split(' ')
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(' ')
)
</script>
This tip is taken from the official style guide.