1. 微信小程序的wxml里不支持includes,indexOf,findIndex等方法微信小程序的wxml里不支持includes
微信小程序的wxml里不支持includes,indexOf,findIndex等方法
2. 在微信小程序类似vuex的扩展 mobx-miniprogram-bindings
store/count.js
import {
observable,
action
} from 'mobx-miniprogram'
// 创建实例
export const store = observable({
// 定义需要存储的字段
count1: 1,
count2: 2,
// 计算属性:用get定义,(只读不能修改)
get count_sum() {
return this.count1 + this.count2
},
// action方法:用来修改store中的值
updateCount1: action(function (step) {
this.count1 += step
})
})
页面绑定
import { createStoreBindings } from "mobx-miniprogram-bindings"
import { store } from './store/count.js'
Page({
// 点击事件
addCount(e) {
// 直接调用下面初始化绑定的方法
this.updateCount1(e.target.dataset.addstep)
},
onLoad: function () {
// 初始化绑定
this.storeBindings = createStoreBindings(this, {
store, // count.js 导出的实例
fields: ['count1'], // 将哪些字段绑定到此页面中进行使用
actions: ['updateCount1'] // 将哪些方法绑定到此页面中
})
},
onUnload: function () {
// 页面卸载的销毁,不然内存泄漏
this.storeBindings.destroyStoreBingds()
},
})
<!-- 上面初始化挂载的字段 -->
<view>{{count1}} + {{count2}} = {{count_sum}}</view>
<button bindtap="addCount" data-addStep='{{2}}'>count1 +1</button>
组件绑定
import { storeBindingsBehavior } from "mobx-miniprogram-bindings"
import { store } from './store/count.js'
Component({
behaviors:[storeBindingsBehavior], // 通过 storeBindingsBehavior 来实现自动绑定
storeBindings:{
store,// 指定要绑定的 store
fields: ['count1'],
actions: ['updateCount1']
}
})
3. 全局重复点击
/**
* 防止重复点击
* @param {调用方法} fn
*/
preventActive(fn, time) {
time = time || 2000;
const self = this
if (this.globalData.pageActive) {
this.globalData.pageActive = false
if (fn) fn()
setTimeout(() => {
self.globalData.pageActive = true
}, time);
} else {
console.log("重复点击")
}
},
4. iphonex等机型安全区域
wx.getSystemInfo({
success: e => {
this.globalData.statusBarHeight = e.statusBarHeight
// 页面高度
this.globalData.screenHeight = e.screenHeight;
// tab高度(48是我自定义组件的高度)
this.globalData.tabBarHeight = 48
// 安全区域高度
this.globalData.safeAreaHeight = e.screenHeight - e.safeArea.bottom
// 状态栏顶部高度(包括药丸按钮)
let capsule = wx.getMenuButtonBoundingClientRect();
if (capsule) {
this.globalData.Custom = capsule;
this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
} else {
this.globalData.CustomBar = e.statusBarHeight + 48;
}
}