TS 请求接口获取数据写法2

<script setup lang="ts">
import { ref } from 'vue'
import axios from 'axios'
import { type Dynamic } from './homeview'
// type imagesArr = {
//   url: string
// }
//3  DynamicObjLists对象中包含一个数组imagesArr 具有一个string类型的url
// type DynamicObjLists = {
//   createdAt: string
//   deletedAt: string
//   id: number
//   text: string
//   updatedAt: string
//   images: imagesArr[]
// }
//2 DynamicObj里面包含两个属性,一个total 一个list:包含DynamicObjLists对象的数组
// type DynamicObj = {
//   total: number
//   list: DynamicObjLists[]
// }
//1 最外层 包含一个大对象 DynamicObj
// type Dynamic = {
//   code: number
//   message: string
//   result: DynamicObj
// }
let dataDynamic = ref({} as Dynamic)
async function getQQdetails() {
  const res = await axios.get<Dynamic>('https://api.apiopen.top/api/getDynamic?page=1&size=10')
  dataDynamic.value = res.data
}

getQQdetails()
</script>
<template>
  <h3>第一层数据</h3>
  <h3>
    {{ dataDynamic.code }}
  </h3>
  <h3>
    {{ dataDynamic.result }}
  </h3>
  <h3>
    {{ dataDynamic.message }}
  </h3>
  <h3>第二层数据</h3>
  <h3>
    {{ dataDynamic.result.total }}
  </h3>
  <h3>
    {{ dataDynamic.result.list }}
  </h3>

  <h3>第三层数据</h3>
  <h3>{{ dataDynamic.result.list[0] }}</h3>
  <h3>
    {{ dataDynamic.result.list[0].updatedAt }}
  </h3>
  <h3>四层数据</h3>
  <h3>{{ dataDynamic.result.list[0].images[0].url }}</h3>
</template>
<style scoped></style>

2 ts页面

export type imagesArr = {
  url: string
}
//3  DynamicObjLists对象中包含一个数组imagesArr 具有一个string类型的url
export type DynamicObjLists = {
  createdAt: string
  deletedAt: string
  id: number
  text: string
  updatedAt: string
  images: imagesArr[]
}
//2 DynamicObj里面包含两个属性,一个total 一个list:包含DynamicObjLists对象的数组
export type DynamicObj = {
  total: number
  list: DynamicObjLists[]
}
//1 最外层 包含一个大对象 DynamicObj
export type Dynamic = {
  code: number
  message: string
  result: DynamicObj
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小云儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值