如果要在跳转到二级页面时需要显示的数据不在一个接口中,可以先在一级页面中请求多个接口,将数据保存到本地或者将多个接口的数据合并到一个对象中,然后将这个对象传递给二级页面。在二级页面中使用传递过来的数据即可。

代码示例:

一级页面:

<template>
  <view>
    <!-- 请求接口1 -->
    <request url='/api/data1' @success='getData1' />

    <!-- 请求接口2 -->
    <request url='/api/data2' @success='getData2' />

    <!-- 跳转到二级页面 -->
    <navigator url='/pages/secondPage/secondPage?data={{data}}' />
  </view>
</template>

<script>
export default {
  data() {
    return {
      data: {} // 保存请求到的数据
    }
  },
  methods: {
    // 接口1请求成功
    getData1(response) {
      this.data.data1 = response.data
    },
    // 接口2请求成功
    getData2(response) {
      this.data.data2 = response.data
    }
  }
}
</script>

二级页面:

<template>
  <view>
    <text>{{data.data1}}</text>
    <text>{{data.data2}}</text>
  </view>
</template>

<script>
export default {
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  }
}
</script>
Uniapp 多接口数据传递 - 如何在二级页面显示不同接口数据

原文地址: https://www.cveoy.top/t/topic/m8Wx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录