0 Tage Serie
javascript / beginner
Snippet

Dynamische Attribut-Bindung

Die Doppelpunkt-Kurzschreibweise (:) wird verwendet, um HTML-Attribute wie 'src' oder 'alt' dynamisch an JavaScript-Variablen zu binden.

snippet.js
javascript
1
2
3
4
5
6
7
8
<template>
<img :src="imagePath" :alt="description">
</template>
<script setup>
const imagePath = '/logo.png';
const description = 'Company Logo';
</script>
vue
Erklärung
1
:src="imagePath"
Bindet das src-Attribut des Bildes an die Variable imagePath.
2
const imagePath = '/logo.png';
Definiert die String-Variable, die für die Bildquelle verwendet wird.