Skip to content

Instantly share code, notes, and snippets.

@casgin
Created November 29, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casgin/550b56bc5fc312168a3703ec9f0f3ad5 to your computer and use it in GitHub Desktop.
Save casgin/550b56bc5fc312168a3703ec9f0f3ad5 to your computer and use it in GitHub Desktop.
Piccolo componente di esempio che mosta una computed property - LaravelDay 2018
<template>
<div>
<fieldset>
<legend>Sommatoria</legend>
<input type="text" v-model="numeroDaSommare" @keyup.enter="aggiungiNumero">
<button v-on:click="aggiungiNumero">Add</button>
</fieldset>
<!-- computed property --->
<h2>Totale: {{Sommatoria}}</h2>
<!-- v-for -->
<ul>
<li v-for="numero in elencoNumeri">
{{numero}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: "Sommatoria",
// === metodo che definisce i dati dell'applicativo
data() {
return {
numeroDaSommare: 0,
elencoNumeri: []
}
},
methods: {
aggiungiNumero() {
this.elencoNumeri.push(parseInt(this.numeroDaSommare));
}
},
computed: {
Sommatoria() {
let totale=0;
this.elencoNumeri.forEach(function(numero){
totale+=numero;
});
return totale;
}
}
}
</script>
<style scoped>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment