微信小程序入门与实战(7)——使用自定义组件改写posts页面

在这里插入图片描述

wxml

<view bind:tap="onTap" class="post-container">
  <view class="post-author-date">
    <image class="post-author" src="{{res.avatar}}"></image>
    <text class="post-date">{{res.date}}</text>
  </view>
  <text class="post-title">{{res.title}}</text>
  <image class="post-image" src="{{res.imgSrc}}"></image>
  <text class="post-content">{{res.content}}</text>
  <view class="post-like">
    <image class="post-like-image" src="../../img/icon/chat.png"></image>
    <text class="post-like-font">{{res.reading}}</text>
    <image class="post-like-image" src="../../img/icon/view.png"></image>
    <text class="post-like-font">{{res.collection}}</text>
  </view>
</view>

wxss

/* components/posts/index.wxss */
.post-container{
  display: flex;
  flex-direction: column;
  border-top:1px solid #ededed;
  border-bottom:1px solid #ededed;
  margin-top: 20rpx;
  background-color: #fff;
}
.post-author-date{
  display: flex;
  flex-direction: row;
  margin: 10rpx 0 20rpx 10rpx;
  align-items: center;
}
.post-author{
  height: 60rpx ;
  width: 60rpx;
}
.post-date{
  margin-left: 20rpx;
  font-size: 26rpx;
}
.post-title{
  font-size: 34rpx;
  font-weight: 600;
  margin-left: 10rpx;
  margin-bottom: 10rpx;
  color:#333;
}
.post-image{
  width: 100%;
  height: 340rpx;
  margin-bottom: 15rpx;
}
.post-content{
  font-size: 28rpx;
  color:#666;
  letter-spacing: 2rpx;
  margin-left: 20rpx;
  margin-bottom: 20rpx;
}
.post-like{
  display: flex;
  flex-direction: row;
  font-size: 26rpx;
  margin-left: 20rpx;
  align-items: center;
}
.post-like-image{
  height: 32rpx;
  width: 32rpx;
  margin-right: 16rpx;
}
.post-like-font{
  margin-right: 40rpx;
}

js

// components/posts/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    res:Object
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onTap(event){
      console.log(event);
      const pid = this.properties.res.postId
      this.triggerEvent('posttap',{
        pid
      })
    }
  }
})

posts页面
json

{
  "usingComponents": {
    "posts":"/components/posts/index"
  }
}

wxml

<!--pages/posts/posts.wxml-->
<swiper indicator-dots="{{true}}" vertical="{{false}}" autoplay="{{true}}" intervsl="3000">
  <swiper-item>
    <image src="/img/wx.png" />
  </swiper-item>
  <swiper-item>
    <image src="/img/vr.png" />
  </swiper-item>
  <swiper-item>
    <image src="/img/iqiyi.png" />
  </swiper-item>
</swiper>

<block wx:for="{{postLists}}" wx:for-item="item">
  <posts bind:posttap="onPostTap" res="{{item}}"></posts>
  <!--
  <view catchtap="onPostTap" data-postId="{{item.postId}}">
    <view class="post-container">
      <view class="post-author-date">
        <image class="post-author" src="{{item.avatar}}"></image>
        <text class="post-date">{{item.date}}</text>
      </view>
      <text class="post-title">{{item.title}}</text>
      <image class="post-image" src="{{item.imgSrc}}"></image>
      <text class="post-content">{{item.content}}</text>
      <view class="post-like">
        <image class="post-like-image" src="../../img/icon/chat.png"></image>
        <text class="post-like-font">{{item.reading}}</text>
        <image class="post-like-image" src="../../img/icon/view.png"></image>
        <text class="post-like-font">{{item.collection}}</text>
      </view>
    </view>
  </view>
  -->
</block>

wxss

/* pages/posts/posts.wxss */
swiper{
  width:100%;
  height:600rpx;
}

swiper image{
  width: 100%;
  height:600rpx;
}
/*
.post-container{
  display: flex;
  flex-direction: column;
  border-top:1px solid #ededed;
  border-bottom:1px solid #ededed;
  margin-top: 20rpx;
  background-color: #fff;
}
.post-author-date{
  display: flex;
  flex-direction: row;
  margin: 10rpx 0 20rpx 10rpx;
  align-items: center;
}
.post-author{
  height: 60rpx ;
  width: 60rpx;
}
.post-date{
  margin-left: 20rpx;
  font-size: 26rpx;
}
.post-title{
  font-size: 34rpx;
  font-weight: 600;
  margin-left: 10rpx;
  margin-bottom: 10rpx;
  color:#333;
}
.post-image{
  width: 100%;
  height: 340rpx;
  margin-bottom: 15rpx;
}
.post-content{
  font-size: 28rpx;
  color:#666;
  letter-spacing: 2rpx;
  margin-left: 20rpx;
  margin-bottom: 20rpx;
}
.post-like{
  display: flex;
  flex-direction: row;
  font-size: 26rpx;
  margin-left: 20rpx;
  align-items: center;
}
.post-like-image{
  height: 32rpx;
  width: 32rpx;
  margin-right: 16rpx;
}
.post-like-font{
  margin-right: 40rpx;
}
*/

js

// pages/posts/posts.js

import {postLists} from '../../data/data.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      postLists
    })
    // console.log(postLists)
  },
  onPostTap(event){
    console.log("posts.js中的onPostTap方法:event——",event);
    var postId = event.currentTarget.dataset.postid||event.detail.pid;
    console.log(postId);
    wx.navigateTo({
      url: 'post-detail/post-detail?id='+postId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

问题:
使用自定义组件重写posts页面,自定义组件中给定的pid值为什么在event.detail.pid里

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值