Vue exercise
by disfated
HTML
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="exercise">
<!-- 1) Hook up the button to toggle the display of the two paragraphs. Use both v-if and v-show and inspect the elements to see the difference -->
<div>
<button @click="visible1=!visible1">Toggle</button>
<p v-if="visible1">You either see me ...</p>
<p v-show="visible1">...or me</p>
<p>You either see me ...</p>
<p>...or me</p>
</div>
<!-- 2) Output an <ul> of array elements of your choice. Also print the index of each element. -->
<ul>
<li v-for="(item, index) in array">{{ index + 1 }}. {{ item }}</li>
</ul>
<!-- 3) Print all key-value pairs of the following object: {title: 'Lord of the Rings', author: 'J.R.R. Tolkiens', books: '3'}. Also print the index of each item. -->
<ul>
<li v-for="(value, key, index) in myObject" :key="key">
<b>{{ key }}</b>: {{ value }} (<a href="#">{{ index }}</a>)
</li>
</ul>
<!-- 4) Print the following object (only the values) and also create a nested loop for the array: {name: 'TESTOBJECT', data: [1.67, 1.33, 0.98, 2.21]} (hint: use v-for and v-if to achieve this) -->
<ul>
<li v-for="value in testData">
<span v-if="!Array.isArray(value)">{{ value }}</span>
<ul v-else>
<li v-for="sub in value">{{ sub }}</li>
</ul>
</li>
</ul>
</div>
JavaScript
new Vue({
el: '#exercise',
data: {
visible1: true,
array: ['Max', 'Anna', 'Chris', 'Manu'],
myObject: {
title: 'Lord of the Rings',
author: 'J.R.R. Tolkiens',
books: '3'
},
testData: {
name: 'TESTOBJECT',
id: 10,
data: [1.67, 1.33, 0.98, 2.21]
}
}
});