-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVueJsIntroSample-04.html
41 lines (38 loc) · 1.17 KB
/
VueJsIntroSample-04.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script src="https://unpkg.com/vue"></script>
<div id="app">
<ul>
<li v-for="product in products" v-bind:class="{low: product.quantity <= 2}">
<input type="number" v-model.number="product.quantity">{{ product.quantity}} </input> {{ product.name}} <!-- -->
<span v-if="product.quantity===0"> - OUT OF STOCK</span>
<button @click="product.quantity+=1">Add</button><!-- -->
</li>
</ul>
<h2>Total Inventory: {{totalProducts}}</h2>
</div>
<script>
const app = new Vue(
{
el: '#app',
data: {
products: []
},
created() {
fetch('https://api.myjson.com/bins/74l63')
.then(response => response.json())
.then(json => { this.products = json.products })
},
computed: {
totalProducts() {
return this.products.reduce((sum, product) => {
return sum + product.quantity
}, 0)
}
}
}
)
</script>
<style>
.low {
color: orangered
}
</style>