溫馨提示×

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

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

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

發(fā)布時(shí)間:2020-12-18 11:43:32 來源:億速云 閱讀:295 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章給大家分享的是有關(guān)微信小程序怎么從數(shù)據(jù)庫加載數(shù)據(jù)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

微信小程序如何從數(shù)據(jù)庫加載數(shù)據(jù)

微信小程序要加載網(wǎng)站數(shù)據(jù)庫里面的真實(shí)數(shù)據(jù),有一個(gè)硬性的要求,就是你的網(wǎng)站域名必須是https協(xié)議才行,要不然你第一步服務(wù)器域名配置你都通過不了,具體申請(qǐng)步驟大家自行去申請(qǐng)吧,這里我就不做過多的介紹。下面我就以加載我博客素材最新的6條數(shù)據(jù)為案例來分析,下面是詳細(xì)步驟。

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

微信小程序怎么從數(shù)據(jù)庫加載數(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 style="width:110px;height:80px;" 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>

感謝各位的閱讀!關(guān)于微信小程序怎么從數(shù)據(jù)庫加載數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

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

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

AI