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

小程序模板網(wǎng)

mpvue寫一個CPASS小程序

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

本文是對CPASS項目的技術要點和所踩的坑做一些總結。

項目

一個提供移動辦公場地的小程序平臺。

使用美團mpvue框架, mpvue:1.0.13, mpvue-loader:1.0.15

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

組件(頁面)間通信

四種方式:

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

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

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

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

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

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

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

復制代碼

第三步:在需要監(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');
}復制代碼

swiper選項卡 +  無限加載

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

空態(tài)下:

技術難點:

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

// swiper動態(tài)設置高度,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)時需要)
    wx.createSelectorQuery().select('#tab-list').fields({
      size: true
    }, function (res) {
      tabListHeight = res.height
      wx.getSystemInfo({
        success: function(resp) {
          self.windowHeight = resp.windowHeight - tabListHeight
        }
      })
    }).exec()
    // 獲取單個item高度
    wx.createSelectorQuery().select('#base-item').fields({
      size: true
    }, function (res) {
      self.baseItemHeight = res.height
      resolve()
    }).exec()
  })
}復制代碼

如果頻繁切換swiper會導致卡死,是因為觸摸滑動swiper和點擊選項卡時賦值swiperIndex都會觸發(fā)swiper bindchange 事件,這里做了判斷處理。

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

scroll-view封裝indexList

實現(xiàn)兩種定位方式:點擊定位,按住右側字母indexList滑動定位。

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

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

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

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

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

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

總結

本文只是簡單講一下項目中涉及到的幾處技術要點,歡迎交流。

打一波廣告:

 

贊賞


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