<template>
<div>
<p>Length: {{ arrayLength }}</p>
</div>
</template>
<script>
export default {
data() {
return {
myArray: [1, 2, 3, 4, 5]
};
},
computed: {
arrayLength() {
return this.myArray.length;
}
}
};
</script>
- A Vue single-file component is defined, containing a template and a script section.
- The template section contains a
<div>
element wrapping a <p>
element. - Inside the
<p>
element, there's a text "Length: " followed by a placeholder {{ arrayLength }}
. - The script section starts with exporting a default Vue component.
- Inside
data()
method, there is a property myArray
initialized with an array [1, 2, 3, 4, 5]
. - The
computed
property defines a computed property named arrayLength
. - The
arrayLength
computed property returns the length of the myArray
property using this.myArray.length
. - The value returned by
arrayLength
will dynamically update whenever myArray
changes, displaying the length of the array in the template where {{ arrayLength }}
is referenced.