亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想要遍歷 ID 數組以從 API獲取數據

想要遍歷 ID 數組以從 API獲取數據

catspeake 2022-12-22 15:24:43
在這個應用程序中,我從一個 API 獲取 ID 并將它們傳遞到第二個 API,并嘗試為第二個 API 中的每個 ID 顯示屬性 overviewList.overview.lifeTimeData.energy,但在這個例子中,我只能做是顯示第一個索引的屬性。如何遍歷 ID 數組并顯示每個 ID 的屬性?這是我目前所擁有的實時工作版本,代碼位于組件 > HelloWorld.vue 下。我知道我需要遍歷 ID 數組,但我不知道該怎么做。https://codesandbox.io/live/68gQlN
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

你想做什么不是很清楚。


但是當你這樣做的時候


      axios.get(

        this.host +

          this.domain +

          `/site/` + ids[0] + `/overview?` +

          this.apiKey,

        this.config

      )

您需要為每個 id 權利運行此查詢。您的方法應該更像這樣:


methods: {

  async getSiteDetails() {

    let ids = [];

    await axios.get(

      this.host +

        this.domain +

          "/sites/list?sortProperty=name&sortOrder=ASC&" +

        this.apiKey,

      this.config

    )

    .then((...responses) => {

      this.siteList = responses[0].data;

      ids = this.siteIdList.concat(responses[0].data.sites.site).map(x => x.id);

    })

    

    ids.forEach(this.getOverviewList);

  },

  async getOverviewList(id) {

    axios.get(

      this.host +

        this.domain +

        `/site/` + id + `/overview?` +

        this.apiKey,

      this.config

    )

    .then(((...responses) => {

        this.overviewList[id] = responses[0].data;

    }))

    .catch(errors => {

      console.log(errors);

    });

  },

}


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號