Created
February 5, 2019 21:29
-
-
Save lucamug/fc29f4d7affd039905f1174d3728b0ef to your computer and use it in GitHub Desktop.
Vue example for https://medium.com/@l.mugnaini/things-that-can-go-wrong-without-a-strictly-typed-language-d91d418a53a1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"> | |
<div v-for="product in products"> | |
<input type="number" v-model="product.quantity"> | |
<button @click="product.quantity += 1"> | |
Add 1 | |
</button> | |
{{ product.name }} | |
</div> | |
<p>Total Inventory: {{ totalProducts }}</p> | |
<p>Quantities: {{products.map(function(p){return p.quantity})}}</p> | |
</div> | |
<script src="https://unpkg.com/vue"></script> | |
<script> | |
const app = new Vue({ | |
el: '#app', | |
data: { | |
products: [] | |
}, | |
computed: { | |
totalProducts() { | |
return this.products.reduce((sum, product) => { | |
return sum + product.quantity | |
}, 0) | |
} | |
}, | |
created: function () { | |
fetch('https://api.myjson.com/bins/74l63') | |
.then(response => response.json()) | |
.then(json => { | |
this.products = json.products | |
}) | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment