Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ionic vue.js中的v-for循环中显示函数的返回值?_Javascript_Vue.js_Ionic Framework - Fatal编程技术网

Javascript 如何在ionic vue.js中的v-for循环中显示函数的返回值?

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

因此,我试图通过调用模板v-for部分中的getUserData()函数来显示返回数据。数据已正确注销,但未显示。有人知道这里发生了什么吗

提前谢谢

这是我的模板:

<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

  • 数据中
    添加好友属性
步骤2

  • 在逻辑中处理循环,而不是在模板中
  • 获取每个好友的数据,并将结果添加到
    friends
    数组中
  • 根据您的逻辑,此循环可以位于方法内部,并且可以在
    mounted()
步骤3 -更新模板以使用
朋友
数组

<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>