寫完調用天氣接口的demo之后,小程序調用天氣接口并且渲染在頁面,順便再調用了一下美圖的接口API:
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;}
注意
以上代碼里面每次刷新的時候,都會隨機調用接口數據
因為接口文檔里面有說明,傳0或者不傳會隨機推薦圖片
所以我們在掉接口的時候可以在url后面綁定參數:
url: 'https://api.apiopen.top/getImages?page=1&count=2',
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答