溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序怎么加載數(shù)據(jù)庫真實(shí)數(shù)據(jù)

發(fā)布時(shí)間:2021-02-01 13:37:30 來源:億速云 閱讀:253 作者:小新 欄目:web開發(fā)

這篇文章主要介紹微信小程序怎么加載數(shù)據(jù)庫真實(shí)數(shù)據(jù),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、進(jìn)入小程序后臺配置https服務(wù)器域名

微信小程序怎么加載數(shù)據(jù)庫真實(shí)數(shù)據(jù)

二、程序中寫好調(diào)用的數(shù)據(jù),并返回json格式

//獲取素材列表接口,該方法位于Application\Home\Controller\WeixinController.class.php中
  public function getdownList(){
    $data=M('Material')->field('id,title,path,date,down,description,view')->order('date desc')->limit(6)->select();
    echo json_encode($data);
  }

三、調(diào)用數(shù)據(jù)

因?yàn)槲业南螺d模板是在index中,所有邏輯代碼要寫在index.js中,下面是具體的代碼

/**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function () {
    console.log('onLoad')
    var that = this
    wx.request({
      url: 'https://www.100txy.com/weixin/getdownlist', //真實(shí)的接口地址
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
 
        console.log(res.data)
        that.setData({  
          Industry: res.data //設(shè)置數(shù)據(jù)
        })  
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

四、在列表模板渲染數(shù)據(jù)

進(jìn)入到index.wxml中加載數(shù)據(jù),具體代碼如下

<view class="newsInfo">
   <block wx:for="{{Industry}}" >
    <view class="newsList" wx:for-index="idx"  bindtap="showDetail" id="{{item.id}}">
      <view class="pic">
        <image  src="https://www.100txy.com/{{item.path}}"></image>
      </view>
      <view class="news_title">
        <text class="title_subject">{{item.title}}\n</text>
        <text class="title">{{item.description}}</text><text class="dianping">瀏覽 {{item.view}}  下載 {{item.down}}</text>
      </view>
      </view>
    <view class="hr"></view>
   </block>
</view>

最后效果如下:這就是我博客素材最新的6條數(shù)據(jù),該小程序源碼我已經(jīng)放到了github上了,需要的朋友可以去下載看看。

微信小程序怎么加載數(shù)據(jù)庫真實(shí)數(shù)據(jù)

以上是“微信小程序怎么加載數(shù)據(jù)庫真實(shí)數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI