小程序问题汇总

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;
        }
      }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值