网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網

通過api接口將json數據展現到小程序示例

發布時間:2018-04-21 09:05 所屬欄目:小程序開發教程

實現知乎客戶端的一個重要知識前提就是,要知道怎么通過知乎新聞的接口,來把數據展示到微信小程序端上。

那么我們這一就先學習一下,如何將接口獲取到的數據展示到微信小程序上。

1.用到的知識點

<1> wx.request 請求接口資源(微信小程序api中的發起請求部分) 

<2>swiper 實現輪播圖的組件 

<3>wx:for 循環語句 

<4>微信小程序的基礎知識

2.實現原理

首先,先看一下這個請求函數

 

				
  1. wx.request({
  2. url: '******', //這里填寫你的接口路徑
  3. header: { //這里寫你借口返回的數據是什么類型,這里就體現了微信小程序的強大,直接給你解析數據,再也不用去尋找各種方法去解析json,xml等數據了
  4. 'Content-Type': 'application/json'
  5. },
  6. data: {//這里寫你要請求的參數
  7. x: '' ,
  8. y: ''
  9. },
  10.  
  11. success: function(res) {
  12. //這里就是請求成功后,進行一些函數操作
  13. console.log(res.data)
  14. }
  15. })

3.代碼

分解圖

<1>首先上一段知乎接口數據的json格式中的開頭

 

				
  1. "date":"20161114",
  2. "stories":[
  3. {
  4. "images":[
  5. "http://jb51.net.com/76125c357aa7b0ca6c9cbc41b4a5326d.jpg"
  6. ],
  7. "type":0,
  8. "id":8975316,
  9. "ga_prefix":"111422",
  10. "title":"小事 · 我和你們一樣"
  11. },
  12. {
  13. "images":[
  14. "http://jb51.net/7c908a5940384123fd88287dbc6a2c98.jpg"
  15. ],
  16. "type":0,
  17. "id":8977438,
  18. "ga_prefix":"111421",
  19. "title":"成長嘛,誰說就意味著一定要長大了?"
  20. },

<2>index.js中

 

				
  1. Page({
  2. data: {
  3. duration: 2000,
  4. indicatorDots: true,
  5. autoplay: true,
  6. interval: 3000,
  7. loading: false,
  8. plain: false
  9. },
  10. onLoad: function () {
  11. var that = this//不要漏了這句,很重要
  12. wx.request({
  13. url: 'http://news-at.zhihu.com/api/4/news/latest',
  14. headers: {
  15. 'Content-Type': 'application/json'
  16. },
  17. success: function (res) {
  18. //將獲取到的json數據,存在名字叫zhihu的這個數組中
  19. that.setData({
  20. zhihu: res.data.stories,
  21. //res代表success函數的事件對,data是固定的,stories是是上面json數據中stories
  22.  
  23. })
  24. }
  25. })
  26.  
  27.  
  28. }
  29. })

<3> index.wxml中

 

				
  1. <view >
  2. <swiper indicator-dots="{{indicatorDots}}"
  3. autoplay="{{autoplay}}" class="banners" interval="{{interval}}" duration="{{duration}}">//這里邊的屬性不重要,看下邊
  4. <block wx:for="{{zhihu}}">
  5. <swiper-item class="banner" >
  6. <image src="{{item.image}}" data-id="{{item.b}}" bindtap="bindViewTap" class="banner-image" width="100%" height="100%"/>
  7. <text class="banner-title">{{item.title}}</text>
  8. </swiper-item>
  9. </block>
  10. </swiper>
  11.  
  12. </view>

看完這個代碼,你會想,根據微信小程序的綁定原理,這里邊的代碼哪里調用了onLoad()這個函數,不用多想,微信小程序給你省略了這些步驟。直接調用zhihu這個數組就行。



易優小程序(企業版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/23828.html 復制鏈接 如需定制請聯系易優客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×