微信小程序入门与实战


在这里插入图片描述

页面间的数据传递和跳转

添加点击事件

自定义属性:
必须以data-开头
多个单词由连接符 “ - ” 连接
单词最好不要有大写字母
多个单词会被转为驼峰命名
例如:post-id转换为postId

属性解析
data-post-id自定义组件属性
module.exports模块导出给其它模块用
<!--pages/featured/featured.wxml-->
<import src="item/item.wxml" />
<view>
  <block wx:for="{{postList}}" wx:for-item="item" wx:for-index="idx">
    <view catchtap="onTapToDetail" data-post-id="{{item.postId}}" >
      <template is="item" data="{{...item}}" />
    </view>
  </block>
</view>

页面跳转处理及传递参数

属性解析
currentTarget事件绑定的当前组件
dataset包含当前组件中所有属性名以data-开头的自定义属性值
import {DBPost} from '../../db/DBPost.js';
Page({

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

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var dbPost=new DBPost();
      this.setData({
        postList:dbPost.getAllPostData()
      })
  },
  onTapToDetail(event){
    var postId=event.currentTarget.dataset.postId;
    console.log(postId);
    wx.navigateTo({
      url: '../post-detail/post-detail?id='+postId,
    })
  },
})

页面获取参数

属性解析
options通过url的key获取对应的value
setNavigationBarTitle设置导航栏标题
// pages/post-detail/post-detail.js
import {
  DBPost
} from '../../db/DBPost.js'
Page({

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

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var postId = options.id;
    this.dbPost = new DBPost(postId);
    this.postData= this.dbPost.getPostItemById().data;
    this.setData({
      post:this.postData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
      wx.setNavigationBarTitle({
        title: this.postData.title,
      })
  },

})

构建文章详情页的wxml

<!--pages/post-detail/post-detail.wxml-->
<view class="container">
  <image class="head_image" src="{{post.postImg}}"></image>
  <text class="title">{{post.title}}</text>
  <view class="author-date">
    <view class="avatar_box">
      <image class="avatar" src="{{post.avatar}}"></image>
      <text class="author">{{post.author}}</text>
    </view>
    <text class="date">{{post.dateTime}}</text>
  </view>
  <text class="detail">{{post.detail}}</text>
  <view class="tool">
    <view class="tool-item">
      <image src="/images/icon/wx_app_like.png" ></image>
      <text class="count">{{post.readingNum}}</text>
    </view>
    <view class="tool-item comment">
      <image src="/images/icon/wx_app_message.png" ></image>
      <text class="count">{{post.commentNum}}</text>
    </view>
    <view class="tool-item">
      <image src="/images/icon/wx_app_collect.png" ></image>
      <text class="count">{{post.collectionNum}}</text>
    </view>
  </view>
</view>

构建文章详情页的wxss

属性解析
space-between组件位于边缘中间留有空白
vertical-align设置元素的垂直对齐方式
transform: scale组件缩放
/* pages/post-detail/post-detail.wxss */
.container{
  display: flex;
  flex-direction: column;
}
.head_image{
  width: 750rpx;
  height: 460rpx;
}
.title{
  font-size: 20px;
  margin: 30rpx;
  letter-spacing: 2px;
  color: #4B556C;
}
.author-date{
  display: flex;
  flex-direction: row;
  margin-top: 15rpx;
  margin-left: 30rpx;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}
.avatar_box{
  display: flex;
  flex-direction: row;
  align-items: center;
}
.avatar{
  width: 50rpx;
  height: 50rpx;
}
.author{
  font-weight: 300;
  margin-left: 20rpx;
  color: #666;
}
.date{
  color: #919191;
  margin-right: 38rpx;
  font-size: 25rpx;
}
.detail{
  color: #666;
  margin: 40rpx 22rpx 0;
  line-height: 44rpx;
  letter-spacing: 1px;
  font-size: 14px;
}
.tool{
  height: 64;
  text-align: center;
  line-height: 64rpx;
  margin: 20rpx 28rpx 20rpx 0;
}
.tool-item{
  display: inline-block;
  vertical-align: top;
  margin-right: 30rpx;
}
.tool-item image{
  width: 30rpx;
  height: 30rpx;
  vertical-align: -3px;
  margin-right: 10rpx;
}
.comment image{
  transform: scale(.85);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值