网络编程
位置:首页>> 网络编程>> JavaScript>> 微信小程序如何调用图片接口API并居中显示

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

作者:祈澈菇凉  发布时间:2023-08-09 15:05:30 

标签:微信,小程序,调用,图片,接口,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',

来源:https://www.jianshu.com/p/c5f971e7f252

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com