I have an array of objects which contain data, I am displaying each object using v-for
. When clicking on each object it have to display the name of object, for this on clicking object I had made a method
with id
as argument so it can display only particular targeted id data, but the name property which I made empty on default it is showing error of undefined property in method of Vue.js, Why I am getting this error?
Html Code
Here the serviceList
is an array of objects and serviceBox(s.id)
is method
I used
<ul>
<li v-for="s in serviceList" v-on:click="serviceBox(s.id)">{{s.mKey}}</li>
</ul>
<p>The Service Name is :{{fService}}</p>
{{fService}}
is the property which I needed to display name
JS Code
<script>
export default {
name: 'app',
data () {
return {
fService: '',
}
},
serviceBox(id){
this.serviceList.forEach(function(s) {
if(id == s.id){
this.fService = s.service;
//here i am getting error cannot set property 'fService' of undefined
}
})
},
}