Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  122] [ 2]  / answers: 1 / hits: 16554  / 7 Years ago, sat, march 18, 2017, 12:00:00

I have a PhoneCard.vue component that I'm trying to pass props to.



<template>
<div class='phone-number-card'>
<div class='number-card-header'>
<h4 class=number-card-header-text>{{ cardData.phone_number }}</h4>
<span class=number-card-subheader>
{{ cardData.username }}
</span>
</div>
</div>
</template>

<script>
export default {
props: ['userData'],
components: {
},
data() {
return {
cardData: {}
}
},
methods: {
setCardData() {
this.cardData = this.userData;
console.log(this.cardData);
}
},
watch: {
userData() {
this.setCardData();
}
}
}


The component receives a property of userData, which is then being set to the cardData property of the component.



I have another Vue.js component that I'm using as a page. On this page I'm making an AJAX call to an api to get a list of numbers and users.



import PhoneCard from './../../global/PhoneCard.vue';
export default {
components: {
'phone-card': PhoneCard
},
data() {
return {
phoneNumbers: [],
}
},
methods: {
fetchActiveNumbers() {
console.log('fetch active num');
axios.get('/api').then(res => {
this.phoneNumbers = res.data;

}).catch(err => {
console.log(err.response.data);
})
}
},
mounted() {
this.fetchActiveNumbers();
}
}


Then once I've set the response data from the ajax call equal to the phoneNumbers property.



After this comes the issue, I try to iterate through each number in the phoneNumber array and bind the value for the current number being iterated through to the Card's component, like so:



<phone-card v-for=number in phoneNumbers :user-data=number></phone-card>


However this leads to errors in dev tools such as property username is undefined, error rendering component, cannot read property split of undefined.



I've tried other ways to do this but they all seem to cause the same error. any ideas on how to properly bind props of a component to the current iteration object of a vue-for loop?


More From » vuejs2

 Answers
24

Try



export default {
props: ['userData'],
data() {
return {
cardData: this.userData
}
}
}

[#58495] Thursday, March 16, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;