Created
November 29, 2018 12:32
-
-
Save casgin/550b56bc5fc312168a3703ec9f0f3ad5 to your computer and use it in GitHub Desktop.
Piccolo componente di esempio che mosta una computed property - LaravelDay 2018
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
<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