I am new to vue and Laravel Framework.
I wanted to fetch data from api and display those data dynamically.
I have created a table named 'Progress' and seeded some data. I have used API resource to fetch data .
I have created a vue template to fetch the data from the api.
this is my template
<template>
<div class="container">
<h1> Goals </h1>
<p id="demo"></p>
<div class= "progress progress-bar-segmented" v-for = "progressdata in progress" v-bind:id="progressdata.id">
<div> {{ progressdata.name }} </div>
<div id="div1" class="progress-bar" style="width:20%">{{ progressdata.goal }}</div>
<div id="div2" class="progress-bar value" style="width:20%">{{ progressdata.value }}</div>
</div>
</div>
</template>
The problem is how to access the individual values like {{ progressdata.goal }} and {{ progressdata.value }} from a progress array in a script?
If I use var a = this.progressdata.goal inside a method. I could receive undefined value
I know that they are accessible only within that scope.. How to access them in a script individually?
this is my script
<script>
export default {
data: function() {
return {
progress: [],
}
},
mounted() {
this.loadContents();
this.start();
setInterval(this.loadContents, 1000);
},
methods: {
loadContents: function() {
//load Api
axios.get('/api/progress')
.then((response) => {
this.progress = response.data.data;
})
.catch(function (error) {
console.log(error);
});
},
start: function() {
var allProgressElements = document.getElementsByClassName('progress');
Array.prototype.forEach.call(allProgressElements, function(el) {
var targetDOM = $(el).getElementsByClassName('value');
targetDOM.innerHTML = json.data[targetDOM.id].value;
});
}
}
}
</script>
Anyone could help please?
thank you.
progressvalue is set, and due to Vue's reactivity, thev-forwill be activated. Do aconsole.login the axios response to check the response. Is the response an array of objects, like you expect?progressarray, and access each item in the array:a = this.progress[0].goala = this.progress[0].goalin a method ..But i recieved TypeError: Cannot read property 'goal' of undefined". How to solve this ? Could you please help?