微信小程序,相关代码

代码 作用
bindtap
bindload 当图片载入完毕时触发,event.detail = {height, width}
wx:if 与 hidden="{{flag ? ture : false}}" 的区别 https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/conditional.html
hidden 是基础组件的公共属性,所有组件默认显示:https://developers.weixin.qq.com/miniprogram/dev/framework/view/component.html#公共属性
wx:for <view wx:key="{{item.content_id}}" wx:for="{{dataList}}">
默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item;
使用 wx:for-item 可以指定数组当前元素的变量名,使用 wx:for-index 可以指定数组当前下标的变量名;
官方文档:https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/list.html
获取小程序用户信息:let { OPENID, APPID, UNIONID } = cloud.getWXContext() https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/userinfo.html
wx.getSystemInfo:获取系统信息 https://developers.weixin.qq.com/miniprogram/dev/api/base/system/system-info/wx.getSystemInfo.html
获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限:wx.getSetting https://developers.weixin.qq.com/miniprogram/dev/api/open-api/setting/wx.getSetting.html
wx.getUserInfo:获取用户信息,跟wx.getSetting配合使用;详见下面“代码1” https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">获取用户信息</button> bindgetuserinfo:用户点击该按钮时,会返回获取到的用户信息,回调的detail数据与wx.getUserInfo返回的一致,open-type="getUserInfo"时有效;https://developers.weixin.qq.com/miniprogram/dev/component/button.html
wx.getShareInfo:获取转发详细信息 https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.getShareInfo.html
获取更多转发信息 通常开发者希望转发出去的小程序被二次打开的时候能够获取到一些信息,例如群的标识。现在通过调用 wx.showShareMenu 并且设置 withShareTicket 为 true ,当用户将小程序转发到任一群聊之后,此转发卡片在群聊中被其他用户打开时,可以在 App.onLaunch 或 App.onShow 获取到一个 shareTicket。通过调用 wx.getShareInfo 接口传入此 shareTicket 可以获取到转发信息。https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.htmlhttps://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onAppShow.html
wx.navigateTo:路由之间传递参数,详见下面代码2 https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
wx.canIUse:判断小程序的API,回调,参数,组件等是否在当前版本可用 <button wx:if="{{ wx.canIUse('button.open-type.getUserInfo') }}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"></button>https://developers.weixin.qq.com/miniprogram/dev/dev_wxwork/dev-doc/qywx-api/foundation/wxqycaniuse.html
显示消息提示框:wx.showToast https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
onShareAppMessage:监听用户点击页面内转发按钮(button 组件 open-type="share")或右上角菜单“转发”按钮的行为,并自定义转发内容 注意:只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮;https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html#onShareAppMessage-Object-object
wx.navigateBack:关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层 https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
客服消息 <button open-type="contact" bindcontact="handleContact"></button>;需要将 button 组件 open-type 的值设置为 contact,当用户点击后就会进入客服会话,如果用户在会话中点击了小程序消息,则会返回到小程序,开发者可以通过 bindcontact 事件回调获取到用户所点消息的页面路径 path 和对应的参数 query。
navigator:页面链接 <navigator open-type="switchTab" url="/pages/xxx">去看看别的页面</navigator>https://developers.weixin.qq.com/miniprogram/dev/component/navigator.html
wx.pageScrollTo:将页面滚动到目标位置,支持选择器和滚动距离两种方式定位 https://developers.weixin.qq.com/miniprogram/dev/api/ui/scroll/wx.pageScrollTo.html
wx.setNavigationBarTitle:动态设置当前页面的标题 https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.setNavigationBarTitle.html
wx.stopPullDownRefresh:停止当前页面下拉刷新 https://developers.weixin.qq.com/miniprogram/dev/api/ui/pull-down-refresh/wx.stopPullDownRefresh.html
wx.chooseImage:从本地相册选择图片或使用相机拍照 https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html
wx.uploadFile:将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html

代码1:

// 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              that.globalData.userInfo = res.userInfo
              console.log(res)

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })

代码2:

页面1中,写:

wx.navigateTo({
        url: `/pages/xxx/index?contentId=xxx`
      })
```
页面2中,获取url中的参数:
```
onLoad: function (options) {
    console.log(options.contentId)
}
```
原文地址:https://www.cnblogs.com/cag2050/p/11834808.html