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

小程序模板網(wǎng)

我是如何從零開始寫出一個微信小程序的

發(fā)布時間:2018-04-26 12:02 所屬欄目:小程序開發(fā)教程

很多人看完bmob快速入門,并完成了簡單的基本配置之后依然不知道如何下手去寫自己的代碼,那么跟著我一起來一步一步做一個小程序吧。(工具:bmob后端云)

新建小程序項目

一、新建項目選擇小程序項目,選擇代碼存放的硬盤路徑,填入你的小程序 AppID,給你的項目起一個好聽的名字,最后,點擊確定,你就得到了你的小程序了開發(fā)者工具傳送門

目錄結(jié)構(gòu)


page
   index
   index.js
   index.wxml
   index.wxss
logs
   logs.js
   logs.json
   logs.wxml
   logs.wxss
utils
   util.js
app.js
   app.json
   app.wxss
   project.config.json

下載和安裝BmobSDK

一、把"bmob-min.js"和"underscore.js"放到相應(yīng)的文件,例如放到utils中

二、接著是在app.js中加入下面兩行代碼進行全局初始化


const Bmob = require('utils/bmob.js');
Bmob.initialize("你的Application ID", "你的REST API Key");

數(shù)據(jù)庫的搭建

一、創(chuàng)建一個名字為detail的表,然后點擊添加列創(chuàng)建3個字段,一個一個的添加

title字段,String 類型,用于存放文章的標(biāo)題 image字段,String 類型,用于存放文章的圖片 detail字段 String類型,用于存放文章的正文 然后添加一些數(shù)據(jù)進行測試

列表頁面實現(xiàn)

一、去到index.js中 Ctrl + A然后按Delete清空這個頁面,然后自己來寫邏輯吧,首先我們需要引入bmob.js 然后在onLoad小程序生命周期中去請求detail表中的數(shù)據(jù),讓bmob和小程序完成第一次交互


//index.js
//獲取應(yīng)用實例
const Bmob = require('../../utils/bmob.js'); //每個需要使用到bmob的頁面都需要引入這個js

Page({
  onLoad() { 
    let Diary = Bmob.Object.extend("detail"); //引入detail這張表
    let query = new Bmob.Query(Diary);
    query.find({
      success: (results) => {
        console.log(results)//打印數(shù)據(jù)
      },
      error(error) {
        console.log(`查詢失敗: ${error.code } ${error.message}`);
      }
    });
  },
})

這里完成了一次對bmob的數(shù)據(jù)查詢,bmob文檔傳送門, 這個查詢默認返回10條記錄。當(dāng)數(shù)據(jù)多了之后,一次查詢很多數(shù)據(jù),這樣是不友好的,并不是說bmob查詢數(shù)據(jù)慢,而是指如果將來你的用戶在網(wǎng)速比較慢的情況使用你的小程序,請求數(shù)據(jù)等待時間過長,這個等待的過程也許會導(dǎo)致用戶不再使用你的小程序。所以我們需要優(yōu)化用戶體驗。那么將代碼改造成一上拉加載更多。


//index.js
//獲取應(yīng)用實例
const app = getApp();
const Bmob = require('../../utils/bmob.js'); //每個需要使用到bmob的頁面都需要引入這個js

Page({
  data: {
    detail:[], //頁面數(shù)據(jù)
    pagination:0, //頁碼
    pageSize: 4, //每頁數(shù)據(jù)
    nodata:true //無數(shù)據(jù)
  },
  onLoad() { 
    //初始頁面第一次獲取頁面數(shù)據(jù)
    this.getData(); 
  },
  getData(){
    let Diary = Bmob.Object.extend("detail"); //引入detail這張表
    let query = new Bmob.Query(Diary);
    query.limit(this.data.pageSize); //返回n條數(shù)據(jù)
    query.skip(this.data.pageSize * this.data.pagination); //分頁查詢
    query.descending('createdAt'); //已created列倒序
    query.find({
      success: (results) => {
        let data = [];
        //將得到的數(shù)據(jù)存入數(shù)組
        for (let object of results) {
          data.push({
            id: object.id,
            title: object.get('title'),
            image: object.get('image'),
            detail: object.get('detail'),
            createdAt: app.timeago(object.createdAt) //調(diào)用timeagoJs把日期轉(zhuǎn)成N天前格式
          })
        }
        //判斷是否有數(shù)據(jù)返回
        if(data.length){
          let detail = this.data.detail; //得到頁面上已經(jīng)存在的數(shù)據(jù)(數(shù)組)
          let pagination = this.data.pagination; //獲取當(dāng)前分頁(第幾頁)
          detail.push.apply(detail, data); //將頁面上面的數(shù)組和最新獲取到的數(shù)組進行合并
          pagination = pagination ? pagination+1 : 1; //此處用于判斷是首次渲染數(shù)據(jù)還是下拉加載渲染數(shù)據(jù)

          //更新數(shù)據(jù)
          this.setData({
            detail: detail,
            pagination: pagination
          })
        }else{
          //沒有返回數(shù)據(jù),頁面不再加載數(shù)據(jù)
          this.setData({
            nodata: false
          })
        }
      },
      error(error) {
        console.log(`查詢失敗: ${error.code } ${error.message}`);
      }
    });
  },
  router(e) {
    //跳轉(zhuǎn)至文章詳情頁
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../detail/detail?id=${id}`
    })
  },
  onReachBottom(){
    //下拉觸底加載更多數(shù)據(jù)
    this.getData();
  }
})

上述代碼中在日期處使用了timeagoJs下載地址,下載timeagoJs放到util文件夾中,然后在app.js中引入。


//app.js
const Bmob = require('./utils/bmob.js')
const timeago = require("./utils/timeago.min.js");
Bmob.initialize("你的Application ID", "你的REST API Key");

App({
  timeago(date){
    return new timeago().format(date, 'zh_CN')
  }
})

二、完成了列表頁邏輯層之后,去到index.wxml編寫視圖層,視圖層就簡單許多了,得到的數(shù)據(jù)是一個數(shù)組,數(shù)組里面是一個json,用wx:for方法把它渲染出來就好了


<!--index.wxml-->
<view class='container'>
  <view class='pane' wx:for='{{detail}}' wx:key="index" data-id='{{item.id}}' bindtap='router'>
    <image src='{{item.image}}'mode='aspectFill'></image>
    <view class='content'>
      <text class='title'>{{ item.title }}</text>
      <text class='date'>{{ item.createdAt }}</text>
    </view>
  </view>
  <view wx:if='{{!nodata}}' class='nodata'>沒有更多數(shù)據(jù)了</view>
</view>

三、來對頁面進行一些美化,編寫一樣樣式吧。畢竟這是一個看臉的社會


/**index.wxss**/
.container{ padding: 30rpx;}
.pane{ width: 100%; margin-bottom:30rpx; border-radius: 5rpx; overflow: hidden; box-shadow: 0 0 10rpx rgba(0, 0, 0, .1) }
.pane image{ width: 100%; height: 240rpx; display: block;}
.pane .content{ background-color: #FFF; padding: 20rpx;}
.pane .title{ display: block; font-size: 30rpx; font-weight: bold; margin-bottom: 20rpx;}
.pane .date{ display: block; font-size: 24rpx; color: #999}
.nodata{ text-align: center; font-size: 24rpx; color: #999}

如果你不喜歡寫這些ui布局,或者前端ui,css比較差,可以直接用別人寫好的現(xiàn)成的樣式傳送門。

以上列表頁面算是完成了。此時點擊頁面的時候,應(yīng)該會報錯,提示detail頁面未配置,那來到app.json里面配置一下detail這個頁面。文章的id已經(jīng)傳過來了,文章的詳情頁就當(dāng)是自己的一個小練習(xí),熟悉bmob吧。


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