微信小程序如何调用图片接口API并居中显示

写完调用天气接口的demo之后,小程序调用天气接口并且渲染在页面,顺便再调用了一下美图的接口API:

美图API

url

wxml:

<view class=\'imagesize\' wx:for=\"{{list}}\" wx:key=\"index\">
<image src=\"{{list.img}}\" class=\'in-image\' >
</image>
</view>

js:

Page({
 data: {
  list: []
 },
 onLoad: function (options) {
  wx.request({
   url: \'https://api.apiopen.top/getImages\',
   header: {
    \'content-type\': \'application/json\'
   },
   success: res => {
    console.log(res.data)
    this.setData({
     //第一个data为固定用法,第二个data是json中的data
     list: res.data.result[0],
    })
   }
  })
 },
})

css:

.imagesize {
 display: flex;
 justify-content: center;
}
.imagesize image {
 width: 400rpx;
 height: 400rpx;
}

微信小程序如何调用图片接口API并居中显示

注意

以上代码里面每次刷新的时候,都会随机调用接口数据

因为接口文档里面有说明,传0或者不传会随机推荐图片

微信小程序如何调用图片接口API并居中显示

所以我们在掉接口的时候可以在url后面绑定参数:

url: \'https://api.apiopen.top/getImages?page=1&count=2\',

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容