微信小程序学习之for循环

摘要:
",reading:"112",collection:"96",}]this.setData},对应的wxss文件swiper{100%;height:600rpx;}swiperimage{100%;height:600rpx;}.post-container{display:flex;flex-direction:column;margin-top:20rpx;margin-bottom:40rpx;background-color:#fff;border-bottom:1pxsolid#ededed;border-top:1pxsolid#ededed;padding-bottom:5px;}.post-author-date{margin:10rpx020rpx10rpx;}.post-author{60rpx;height:60rpx;vertical-align:middle;}.post-date{margin-left:20rpx;vertical-align:middle;font-size:26rpx;}.post-title{font-size:34rpx;font-weight:600;color:#333;margin-bottom:10px;margin-left:10px;}.post-image{margin-left:16px;100%;height:340rpx;margin:auto0;margin-bottom:15px;}.post-content{color:#666;font-size:28rpx;margin-bottom:20rpx;margin-left:20rpx;letter-spacing:2rpx;line-height:40rpx;}.post-like{font-size:13px;display:flex;flex-direction:row;line-height:16px;margin-left:10px;}.post-like-image{height:16px;16px;margin-left:8px;vertical-align:middle;}.post-like-font{vertical-align:middle;margin-right:20px;}二、将数据改成到js文件中创建data/posts-data.js文件varposts_content=[{date:"Sep182017",title:"飞蟹1",imgSrc:"/images/post/crab.png",avatar:"/images/avatar/1.png",content:"现在的飞蟹正好吃。。。。。。

一、使用自定义创建的json数据

1. 创建微信小程序项目后

在wxml文件中增加for循环

<block wx:for="{{posts_key}}" wx:for-item="item" wx:for-index="idx">
    <view class="post-container">
      <view class="post-author-date">
       <image   src="http://t.zoukankan.com/{{item.avatar}}"></image>
        <text class="post-date">{{item.date}}</text>
     </view>
      <text class="post-title">{{item.title}}</text>
      <image   src="http://t.zoukankan.com/{{item.imgSrc}}"></image>
      <text class="post-content">{{item.content}}</text>
      <view class="post-like">
       <image   src="http://t.zoukankan.com/images/icon/chat.png"></image>
        <text class="post-like-font">{{item.collection}}</text>
        <image   src="http://t.zoukankan.com/images/icon/view.png"></image>
        <text class="post-like-font">{{item.reading}}</text>
      </view>
   </view>
  </block>
posts_key的定义在js文件中
 onLoad: function (options) {
      //页面初始化 options为页面跳转所带来的参数
      var posts_content = [
        {
          date: "Sep 18 2017",
          title: "飞蟹1",
          imgSrc:"/images/post/crab.png",
          avatar:"/images/avatar/1.png",
          content:"现在的飞蟹正好吃。。。。。。",
           reading:"112",
          collection:"96",
        },
        {
          date: "Sep 18 2017",
          title: "飞蟹2",
          imgSrc: "/images/post/bl.png",
          avatar: "/images/avatar/2.png",
          content: "现在的飞蟹正好吃。。。。。。",
          reading: "112",
          collection: "96",
        }
      ]
      this.setData({
        posts_key: posts_content
      })
  },

对应的wxss文件

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

二、将数据改成到js文件中

创建data/posts-data.js文件

var posts_content = [
  {
    date: "Sep 18 2017",
    title: "飞蟹1",
    imgSrc: "/images/post/crab.png",
    avatar: "/images/avatar/1.png",
    content: "现在的飞蟹正好吃。。。。。。",
    reading: "112",
    collection: "96",
  },
  {
    date: "Sep 18 2017",
    title: "飞蟹2",
    imgSrc: "/images/post/bl.png",
    avatar: "/images/avatar/2.png",
    content: "现在的飞蟹正好吃。。。。。。",
    reading: "112",
    collection: "96",
  }
]
var name = "zhangsan"
module.exports = {
  postList:posts_content,
  exportName:name
}

在posts.js中引用posts-data.js文件

微信小程序学习之for循环第1张

在onLoad函数里, 将data.posts_keys 赋值为postsData.postList

  onLoad: function (options) {
    this.data.posts_key = postsData.postList;
  },

最终效果是一致的。

免责声明:文章转载自《微信小程序学习之for循环》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇lin-cms-dotnetcore.是如何方法级别的权限控制的?InnoDB关键特性之double write下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

基于Web Service的客户端框架搭建一:C#使用Http Post方式传递Json数据字符串调用Web Service

引言   前段时间一直在做一个ERP系统,随着系统功能的完善,客户端(CS模式)变得越来越臃肿。现在想将业务逻辑层以下部分和界面层分离,使用Web Service来做。由于C#中通过直接添加引用的方来调用Web Service的方式不够灵活,故采取手动发送Http请求的方式来调用Web Service。最后选择使用Post方式来调用Web Service,...

ASP.NET Web API(一):使用初探,GET和POST数据

概述 REST(Representational State Transfer表述性状态转移)而产生的REST API的讨论越来越多,微软在ASP.NET中也添加了Web API的功能。 我们看dudu的文章HttpClient + ASP.NET Web API, WCF之外的另一个选择知道了博客园也开始使用了Web API,且在使用Web API B...

c语言处理通过ajax发起http的post请求CGI并向浏览器返会值

环境:centos 6.5 web容器:apache2.4,[<http://httpd.apache.org/docs/2.4/] 准备: cd /usr/local/httpd-2.4.20/modules/generators enter   cp mod_cgi.c /usr/local/apache/bin enter   ....

CSS3实现10种Loading效果(转)

CSS3实现10种Loading效果 原文地址:http://www.cnblogs.com/jr1993/p/4622039.html 昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> <sp...

for循环结构,break和continue的使用

一.for 循环       语法:        for(表达式一;表达式二;表达式三){        循环操作       }       表达式一:参数初始化       表达式二:条件判断       表达式三:更新循环变量       执行流程:         步骤一:执行表达式一.初始化参数        步骤二:执行表达式二进行循环条件的...

shell脚本编程基础之while、for、until循环

while及until循环结构 while CONDITION;do statement done 进入循环:条件满足 退出循环:条件不满足 当需要命令的执行状态返回值时,可以直接把整个命令当做循环的条件 until CONDITION;do statement ... done 进入循环:条件不满足 退出循环:条件不满足 退出循环的...