Javascript 如何在ionic vue.js中的v-for循环中显示函数的返回值?
因此,我试图通过调用模板v-for部分中的getUserData()函数来显示返回数据。数据已正确注销,但未显示。有人知道这里发生了什么吗 提前谢谢 这是我的模板:Javascript 如何在ionic vue.js中的v-for循环中显示函数的返回值?,javascript,vue.js,ionic-framework,Javascript,Vue.js,Ionic Framework,因此,我试图通过调用模板v-for部分中的getUserData()函数来显示返回数据。数据已正确注销,但未显示。有人知道这里发生了什么吗 提前谢谢 这是我的模板: <template> <ion-page> <NavBar/> <ion-content padding> {{currentuser.vorname}} haha <br/> Friends: <i
<template>
<ion-page>
<NavBar/>
<ion-content padding>
{{currentuser.vorname}} haha
<br/>
Friends:
<ion-list>
<ion-item v-for="friend in this.currentuser.friends" :key="friend"> {{getUserData(friend)}} </ion-item>
</ion-list>
</ion-content>
</ion-page>
</template>
你需要稍微更新一下你的逻辑 步骤1
- 在
添加好友属性数据中
- 在逻辑中处理循环,而不是在模板中李>
- 获取每个好友的数据,并将结果添加到
数组中friends
- 根据您的逻辑,此循环可以位于方法内部,并且可以在
mounted()
朋友
数组
<template>
<ion-page>
<NavBar/>
<ion-content padding>
{{currentuser.vorname}} haha
<br/>
Friends:
<ion-list>
<ion-item v-for="(friend, index) in friends" :key="index"> {{ friend }} </ion-item>
</ion-list>
</ion-content>
</ion-page>
</template>
{{currentuser.vorname}}哈哈
朋友:
{{朋友}
我对ion
一无所知,但看起来getUserData
正在返回undefined
。如果您要返回fetch…
,那么这将返回一个承诺,我猜您需要存储fetch调用的结果,并在数据可用后以某种方式重新呈现应用程序。但是,我对您使用的这个框架一无所知。当/getUserEntrybyEmail
成功时。它是返回一个数组(朋友列表)还是像对象一样返回一个条目?@SimoD'loMafuxwana它返回一个Json@Taer好的,看下面的答案是什么。但是,为了使列表适当地更改,我必须使用Vue.set(this.Friendsdata、key、data);这是.$forceUpdate();而不是推
data() {
return {
friend: []
}
},
for (let a = 0; this.currentuser.friends.length > a; a++) {
const email = this.currentuser.friends[a].email;
fetch("/getUserEntrybyEmail", {
headers: {
'Accept': 'application/json, text/plain, */*',
"Content-type" : "application/json"
},
method: "POST",
body: JSON.stringify( { "email" : email } ),
}).then(response => {
if(response.status==200){
return response.json();
} else {
console.log("Access Denied.")
}
}).then((data)=>{
console.log(data)
// add each result to the friends array
this.friend.push(data);
})
}
<template>
<ion-page>
<NavBar/>
<ion-content padding>
{{currentuser.vorname}} haha
<br/>
Friends:
<ion-list>
<ion-item v-for="(friend, index) in friends" :key="index"> {{ friend }} </ion-item>
</ion-list>
</ion-content>
</ion-page>
</template>