微信小程序总结

1.在js方法获取data的数据,以name为案例进行说明
  

 dada{
        name:"陈紫函",
        age:"22",
        sex:"男"
     }

如果js中想使用name的值,就是:

this.data.name

2.redirectTo携带参数跳转页面以及在跳转的页面接收参数

 goCharge: function(e){ // 充值
    var areaCode = '100'
    var areaName = '中华英雄'
    var hasmoney = ‘90’
    wx.redirectTo({
       url: '../charge/charge?areaCode=' + areaCode + "&&areaName=" + areaName   + "&&hasmoney=" + hasmoney,
     })
  },

在跳转到的页面接收参数:

//在跳转到的页面的onLoad方法中接收,当然在data中定义Name,Code,Money三个变量用来存储接收的参数
  onLoad:function(intoMoney){
      this.setData({
         Name: intoMoney.areaName,
         Code: intoMoney.areaCode,
         Money: intoMoney.hasmoney
      })
   },

3.request请求数据,首先要在data中定义一个变量来接收请求过来的数据

  onLoad: function (options) {
  //这个地方要定义that来接收this
    var that = this
    wx.request({
      url: '',
      data: {},
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      method: 'GET',
      success: function (res) {
       //将请求到的数据放到data数据变量中
       that.setData({
          diancard: data
          //将小区数据进行回显下拉框
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

4.请求数据配置App.js中的全局变量

App({
  globalData: {
    //全局变量openid
    openidx: '',
               }
   });
   
    wx.login({
      success: function(res) {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          var APPID = 'wxc416aa3046bed24b'
          var SECRET = 'ab4e940de98124fc9e4fef58d17dfc5d'
          // var that = this;
          wx.request({
            url: '',
            data: {},
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              getApp().globalData.openidx = res.data.openid
            }
          })
        } else {
          console.log("登陆失败" + res.errMsg)
        }

      }

    })

5.微信小程序自定义变量,以及获取变量值

先看看wxml

<view class='box' bindtap='getValue'>
    <view class='first' data-num="1024" data-name="张三">第一个view</view>
    <view class='second' data-age="2017" data-con="李四">第二个view</view>
    <text >第一个view的自定义属性:{{first}}</text>
    <text >第二个view的自定义属性:{{second}}</text>
</view>

然后是js

 data: {
    first:'',
    second:''
  },
  onLoad: function (options) {},
  getValue(e){
      const that = this;
      that.setData({
        first: e.target.dataset.name,
        second: e.target.dataset.conbn
      })
  }

6.小程序view中的image图片居中显示

<view style="display:flex;justify-content:center;">
      <image style="width:130px;height:110px;margin-top:60px" src='../images/yhqsx.png'> 
      </image>
</view>

点评:给图片一个宽高,然后在view中书写style,通过display:flex;justify-content:center;就可以达到想要的效果

7.获取全局变量数据

getApp().globalData.属性名

全局变量赋值:

var app=getApp();     // 取得全局App

app.globalData.userInfo = res.data.data.uid     // 取得全局变量需要的值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酒书

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值