微信小程序常用

wepy框架

转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

wepy.switchTab

不可以带参数

跳转到某个页面,不关闭当前页面

wepy.navigateTo({
    url: 'test?a=123&b=456'
})

跳转到某个页面,并关闭当前页面

wx.redirectTo({
    url: '../../pages/goodsList/goodsList?key=123&name=ptt',
})

关闭当前页面,返回上一页面或多级页面

可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。

back:function(){
    wepy.navigateBack({
      // 回退前 delta(默认为1) 页面
      delta: 1, 
    })
}

获取当前页面url信息

//获取加载的页面
let pages = getCurrentPages()

//获取当前页面的对象
let currentPage = pages[pages.length-1]

//当前页面url
let url = currentPage.route

//如果要获取url中所带的参数可以查看options
let options = currentPage.options

判断是否小程序

window.wx.ready(function () {
    isWxMini = window.__wxjs_environment === 'miniprogram'
})

pages/* 页面 出现脚本错误或者未正确调用 Page()

wpy未导入 wepy

<script>
  import wepy from 'wepy';
</script>

页面初始化加载ajax列表

<template>
  <view wx:for="items">
    {{item.id}}
  </view>
</template>

<script>
  import wepy from 'wepy';

  export default class Index extends wepy.page {
    onShow() {
      this.getList()
    }

    async getList() {
      let res = await util.get("https://restcountries.eu/rest/v2/all")
      this.items = res.data;
      this.$apply()
    }
  }
</script>

页面标题title

<script>
config = {
      navigationBarTitleText: '分类管理'
}
</script>

navigator 标签

相当于链接

<navigator url="../cart/cart" open-type="switchTab">跳转到购物车页面</navigator>

open-type可选值:

navigate : 对应于wepy.navigateTo
redirect : 对应于wepy.redirectTo
switchTab :对应于wepy.switchTab

检查小程序状态

let wxLoginState =  wepy.checkSession().then(
    res => {
        return true
    }, 
    res => {
        return false
    }
);

import 用 @ 来用为绝对路径

<script>
    import api from '@/config/api';
</script>

api.js 文件位置 src/config/api.js

读写全局变量globalData

在page页面中

//写
this.$parent.globalData.userInfo = {'user_id':1234}
//读
let userInfo = this.$parent.globalData.userInfo;

wepy showModal 使用

wepy.showModal({
      title: 'title',
      content: 'content',
      confirmText: '重新加载'
  }).then(function(res) {
    if (res.confirm) {
      //确认按钮后操作
      console.log('confirm')
    }
  })

获取 app.wpy 顶层全局方法

wepy.$instance
如 wepy.$instance.getUserInfo

组件

input type类型

text:普通文本
number:无小数点的数字键盘
idcard:无小数点,有个 X 键的数字键盘,方便输入身份证号码
digit:有小数点的数字键盘

判断用户开发环境

用 wx.getSystemInfoSync() 判断 systemInfo.platform == 'devtools'