小程序获取用户头像和昵称

  查到一个方法:
wxml代码:


 <view> 
        <image class="userhead" src='{{userInfo.avatarUrl}}'>image>
        <view class="username">{{userInfo.nickName}}view>
view>

js代码:

onLoad: function(options) {
    var that = this;
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })
	},

你可能感兴趣的:(微信小程序)