最近在做微信小程序。
上周的时候就想在首页的帖子部分显示出用户名和头像,但是因为多表联查没整明白,就换了一种办法。
点击一个帖子,跳转到新页面,显示发布该帖子的用户详细信息。
用这种办法需要传递参数,帖子的id和用户的openid。
上代码
js部分:
onPostTap:function(event){
var id =event.currentTarget.dataset.id;
console.log(id)
db.collection('posts').doc(id).get().then(res=> {
console.log('haha',res.data._id,res.data._openid);//输出该帖子的id
app.userInfo._openid = res.data._openid;
console.log('app.userinfo',app.userInfo._openid)
wx.navigateTo({
url: '/pages/mine/alter/alter?_openid='+app.userInfo._openid,
})
})
wxml部分:
<view>
<view class="content">
</view>
<text>发帖人详细信息</text>
<view class="line"></view>
<view class="sub">
<view class='ab' wx:for="{{nu}}"><!--wx:for是微信数据绑定的一种方式,该数组有多少数据就显示多少个view-->
<!--item就相当于数组名+下标的结合体,适用于调用数组所有数据-->
<view>{{item.Academy}}</view>
<view >{{item.StudentClass}}</view>
<view >{{item.StudentName}}</view>
<view >{{item.StudentPhone}}</view>
<view >{{item.userName}}</view>
<image src="{{item.userPhoto}}" class="avatar"></image>
</view>
</view>
</view>
如果是想用按钮响应跳转页面显示,代码如下:
js部分:
var id =event.currentTarget.dataset.id;
console.log(id)
wx.showModal({ // 提示框
title: '提示',
content: '确定同行吗',
success:function(res){
if(res.cancel){
//点击取消,默认隐藏弹框
}else{
db.collection('posts').doc(id).get().then(res=> {
console.log('haha',res.data._id,res.data._openid);//输出该帖子的id
app.userInfo._openid = res.data._openid;
console.log('app.userinfo',app.userInfo._openid)
wx.navigateTo({
url: '/pages/mine/alter/alter?_openid='+app.userInfo._openid,
})
})
}
}
})
}
wxml部分:同上