zoukankan      html  css  js  c++  java
  • 微信小程序(七)文章详情页面动态显示

    文章详情页面动态显示(即点击某个文章就跳转到相应文章的详情页):
    思路:在文章列表页面添加catchtop事件,在js文件中获取文章的index,并用wx.navigateTo中的
    url拼接详情页的地址和文章的index,(在该操作之前该js应该已引入静态数据,在data中定义相应数据,
    并更新数据,在详情页的js中也同样如上,详情看demo),列表页面事件添加完毕后,详情页需要接收数据
    (index和文章内容),这就用到了,详情页的生命周期函数options来接收值.

    为每个文章添加事件:

    <block wx:for='{{listArr}}' wx:key='{{index}}'>
    <view catchtap="toDetail" data-index='{{index}}'>
    <template is='listTmp' data='{{...item}}'/>
    </view>
    </block>

    detail.wxml代码如下:

    <!--pages/detail/detail.wxml-->
    <view class="detailContainer">
    <image class="headImg" src="{{detailObj.detail_img}}"></image>
    <view class="avatar_date">
    <image src="{{detailObj.avatar}}"></image>
    <text>{{detailObj.author}}</text>
    <text>发布于</text>
    <text>{{detailObj.data}}</text>
    </view>
    <text class="company">{{detailObj.title}}</text>
    <view class="collection_share_container">
    <view class="collection_share">
    <image src="/static/images/sc.png"></image>
    <image src="/static/images/share2.png"></image>
    </view>
    <view class="line"></view>
    </view>
    <button>转发此文章</button>
    <text class="content">{{detailObj.detail_content}}</text>
    </view>

    detail.js代码如下:

    // pages/detail/detail.js
    let datas=require('../data/list-data.js');
    console.log(datas);
    Page({
    
    /**
    * 页面的初始数据
    */
    data: {
    datailObj:{},
    index:null
    },
    
    /**
    * 生命周期函数--监听页面加载
    */
    onLoad: function (options) {
    console.log(options);
    //获取list页面传来的参数值
    let index = options.index;
    //更新data中的detailObj的状态值
    this.setData({
    detailObj:datas.list_data[index],
    index //同名参数值可以省略不写(index:index)
    });
    },
    /**
    * 生命周期函数--监听页面初次渲染完成
    */
    onReady: function () {
    
    },
    
    /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {
    
    },
  • 相关阅读:
    二叉树遍历
    php环境搭建工具推荐
    laravel框架包资源分享
    memcached配置
    双引号转义问题
    PHP命名空间
    正则表达式
    2017,起航!
    关于大数据量下进行大数据展示的杂谈
    mysql空数据的处理
  • 原文地址:https://www.cnblogs.com/ly-520/p/10279098.html
Copyright © 2011-2022 走看看