Hi I'm trying to make a website where I have a home and a "Brazil" component. I'm using vue router to switch between home and Brazil. In Brazil there is a calculator but I cant use the methods used in the script tag. The calculator should ask for a grade input and then calculate the Average of it when the user clickes on Average. The calculation is correct. The buttons don't work. Does annyone know what the problem is? Here is the code:
<template>
<div>
Brazil
<h2>Grade-Calculator </h2>
<div id="calculator">
<ul>
<li v-for="(g,idx) in grades" :key="idx">{{idx+1}}. Grade : {{g}}</li>
</ul>
<div>
<label>New Grade: </label>
<input type="text" v-model="newGrade" />
<button v-on:click="addGrade()">Ok</button>
</div>
<br>
<div>
<button v-on:click="calcAvg()">Average</button>
<p>Average: {{ sum }}</p>
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
export default {
name: "Brazil",
props: {}
};
new Vue({
el: '#calculator',
data: {
grades: [],
newGrade: 0,
avg: 0
//TEST
},
methods: {
addGrade: function () {
this.grades.push(this.newGrade)
this.newGrade = 0
},
calcAvg: function () {
let sum = 0;
for (var i = 0; i < this.grades.length; i++) {
let zahl = parseInt(this.grades[i]);
sum = sum + zahl;
}
//calculate average and print it
console.log(sum)
console.log(this.grades.length)
return sum / this.grades.length;
}
}
})
</script>
addGradewithout ()new Vuepart shouldn't be inside the.vuefile. Most of the config options that you're passing to theVueconstructor should be inside your component definition instead. I suggest using Vue CLI to build a skeleton project so you can see how things are supposed to be laid out.