小程序模板網(wǎng)

mpvue寫一個(gè)CPASS小程序

發(fā)布時(shí)間:2018-09-06 09:26 所屬欄目:小程序開發(fā)教程

本文是對CPASS項(xiàng)目的技術(shù)要點(diǎn)和所踩的坑做一些總結(jié)。

項(xiàng)目

一個(gè)提供移動(dòng)辦公場地的小程序平臺。

使用美團(tuán)mpvue框架, mpvue:1.0.13, mpvue-loader:1.0.15

靜態(tài)資源(除了tabbar圖標(biāo))放在阿里云oss

組件(頁面)間通信

四種方式:

  1. Vuex狀態(tài)管理(mapActions,mapGetters)
  2. 本地緩存(setStorage,getStorage,removeStorage)
  3. Bus集中式的事件中間件($emit,$on,$off)
  4. 路由query傳值

這里說一下比較少用的第三種通信方式。Bus應(yīng)用于非父子組件通信,利用$emit,$on,$off分別來分發(fā)、監(jiān)聽、取消監(jiān)聽。

第一步:在mixins(混合)建一個(gè)文件event-bus.js

import Vue from 'vue';
export default new Vue();復(fù)制代碼

第二步:在需要分發(fā)的組件中引入event-bus,再傳遞分發(fā)事件

import Bus from '@/mixins/event-bus'

// 需要傳遞給兄弟組件的值
let params = { 
    ***
}
Bus.$emit('getParams', params)

復(fù)制代碼

第三步:在需要監(jiān)聽的組件中引入event-bus,在created周期去監(jiān)聽事件(小程序周期監(jiān)聽無效),在 beforeDestroy 周期取消監(jiān)聽事件

import Bus from '@/mixins/event-bus'
created () {
  // 監(jiān)聽事件
  Bus.$on('getParams', params => {
    // to do something

  })
},beforeDestroy () {
  // 清除監(jiān)聽
  Bus.$off('getParams');
}復(fù)制代碼

swiper選項(xiàng)卡 +  無限加載

利用微信官方提供的swiper封裝一個(gè)無限數(shù)據(jù)加載的swiperTab選項(xiàng)卡。

空態(tài)下:

技術(shù)難點(diǎn):

swiper需要設(shè)置固定高度,觸底 onReachBottom 無限加載又需要高度。所以需要在swiper標(biāo)簽設(shè)置動(dòng)態(tài)高度 :style="{height: swiperHeight + 'px'}" 。 onLoad 周期獲取單個(gè)list-item的高度。假如所渲染數(shù)據(jù)有n組數(shù)據(jù),則swiper高度為: swiperHeight = baseHeight * n + 加載提示占位高度 。

// swiper動(dòng)態(tài)設(shè)置高度,list為需要渲染的數(shù)據(jù)
autoHeight(list) {
  let num = list.length;
  // this.loadHeight加載提示語的高度
  let listHeight = this.baseItemHeight * num + this.loadHeight
  this.swiperHeight = Math.max(this.windowHeight, listHeight);
},
// 獲取靜態(tài)高度
calcStaticHeight() {
  return new Promise((resolve) => {
    let self = this;
    let tabListHeight; // 獲取tab高度
    // 獲取除去tabList高度,全屏高度(空態(tài)狀態(tài)時(shí)需要)
    wx.createSelectorQuery().select('#tab-list').fields({
      size: true
    }, function (res) {
      tabListHeight = res.height
      wx.getSystemInfo({
        success: function(resp) {
          self.windowHeight = resp.windowHeight - tabListHeight
        }
      })
    }).exec()
    // 獲取單個(gè)item高度
    wx.createSelectorQuery().select('#base-item').fields({
      size: true
    }, function (res) {
      self.baseItemHeight = res.height
      resolve()
    }).exec()
  })
}復(fù)制代碼

如果頻繁切換swiper會(huì)導(dǎo)致卡死,是因?yàn)橛|摸滑動(dòng)swiper和點(diǎn)擊選項(xiàng)卡時(shí)賦值swiperIndex都會(huì)觸發(fā)swiper bindchange 事件,這里做了判斷處理。

// 點(diǎn)擊切換
clickTab (tab) {
  if (this.currentTab === tab) {
    return false;
  } else {
    this.currentTab = tab
    this.allLoaded = false
    this.pageNum = 1
    this.loadTips = '上拉加載更多'
    this.getDataList(this.loadTips);
  }
},復(fù)制代碼

scroll-view封裝indexList

實(shí)現(xiàn)兩種定位方式:點(diǎn)擊定位,按住右側(cè)字母indexList滑動(dòng)定位。

技術(shù)難點(diǎn):按住右側(cè)indexList滑動(dòng)定位,獲取字母indexList的上邊距 offsetTop ,按住滑動(dòng)時(shí)獲取手指距離屏幕頂部的距離 clientY, 手指移動(dòng)距離為 moveY=clientY-offsetTop, 具體實(shí)現(xiàn)如下:

// 索引定位(滑動(dòng)開始) @touchstart="handlerStart"
handlerStart (e) {
  this.targetIndex = e.mp.target.id
},
// 索引定位(滑動(dòng)過程) @touchmove="handlerMove"
handlerMove(e) {
  let keyList = this.keyList;
  // 手指滑動(dòng)垂直距離
  let moveY = e.mp.touches[0].clientY;
  let rY = moveY - this.offsetTop;
  if (rY >= 0) {
    // apHeight為字母表indexList中單個(gè)字母塊高度,計(jì)算結(jié)果向上取整
    let index = Math.ceil((rY - this.apHeight) / this.apHeight);
    if (index >= 0 && index < keyList.length) {
      this.targetIndex = keyList[index];
    }
  } else {
    this.targetIndex = keyList[0]
  }
},復(fù)制代碼

view或者text設(shè)置border-radius=50%有時(shí)候在真機(jī)會(huì)變形(排除flex布局的影響)。

wxml不支持復(fù)雜邏輯,如模版字符串,字符串截取等等。

text設(shè)置行高的時(shí)候會(huì)出現(xiàn)樣式異常,替換成view便可解決此問題。

wx.showLoading和wx.showToast的屬性title不可為空,線上會(huì)報(bào)錯(cuò),影響js執(zhí)行。

總結(jié)

本文只是簡單講一下項(xiàng)目中涉及到的幾處技術(shù)要點(diǎn),歡迎交流。

打一波廣告:

 

贊賞


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