一、使用自定义创建的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 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="../../images/icon/chat.png"></image>
<text class="post-like-font">{{item.collection}}</text>
<image class="post-like-image" src="../../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{
width: 100%;
height:600rpx;
} swiper image{
width: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{
width: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;
width: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;
width: 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文件

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

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

最终效果是一致的。

  

微信小程序学习之for循环的更多相关文章

  1. 微信小程序 学习资料

    微信小程序 学习资料 资料名称 网址 官方教程 https://developers.weixin.qq.com/miniprogram/dev/index.html?t=18110517

  2. 【微信小程序学习笔记】入门与了解

    [微信小程序学习笔记(一)] IDE 下载安装 下载地址 官方工具:https://mp.weixin.qq.com/debug/w … tml?t=1476434678461 下载可执行文件后,可按 ...

  3. 微信小程序学习笔记二 数据绑定 + 事件绑定

    微信小程序学习笔记二 1. 小程序特点概述 没有DOM 组件化开发: 具备特定功能效果的代码集合 体积小, 单个压缩包体积不能大于2M, 否则无法上线 小程序的四个重要的文件 *js *.wxml - ...

  4. 微信小程序学习笔记一 小程序介绍 & 前置知识

    微信小程序学习笔记一 1. 什么是小程序? 2017年度百度百科十大热词之一 微信小程序, 简称小程序, 英文名 Mini Program, 是一种不需要下载安装即可使用的应用 ( 张小龙对其的定义是 ...

  5. 微信小程序学习

    官方网站 https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 项目结构介绍 -- MINA框架 https://mp.weixin.qq.com/ ...

  6. 微信小程序学习笔记(阶段一)

    一阶段学习过程: (一)看官方文档的简易教程:https://mp.weixin.qq.com/debug/wxadoc/dev/ (二)看小码哥视频:https://chuanke.baidu.co ...

  7. 微信小程序学习指南

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. 微信小程序学习简介

    如何向微信小程序导入DEMO源码: 参考方法 参考学习小程序官方文档 小程序官方文档 小程序目录简介 app.json :设置一些工程全局的量.js : 写一些函数逻辑.wxml: 调用.js中写的函 ...

  9. 微信小程序学习笔记以及VUE比较

    之前只是注册了一下微信小程序AppID,随便玩了玩HelloWorld!(项目起手式),但是最近看微信小程序/小游戏,崛起之势不可阻挡.小程序我来了!(果然,一入前端深似海啊啊啊啊啊~) 编辑器: S ...

随机推荐

  1. python-局部变量和全局变量

    name = "feifei" def change_name(name): print("before change name:%s" % name) nam ...

  2. 解决 java.lang.ClassNotFoundException: javax.servlet.ServletContext报错

    原因:tomcat找不到servlet,即缺少了servlet-api.jar包 解决方法: 我的项目是用maven搭建的 在pom.xml中加入依赖 <dependency> <g ...

  3. JetBrains Goland 2018.2.1最新版破解

    JetBrains GoLand 2018.2.1破解版是JetBrains的新商业IDE,旨在为Go开发提供符合人体工程学的环境.新的IDE通过特定于Go语言的编码辅助和工具集成扩展了Intelli ...

  4. web前端优化

    在谈到Web优化之前,我们回到一个更原始的问题,Web前端的本质是什么.我的理解是: 将信息快速并友好的展示给用户并能够与用户进行交互.快速的意思就是在尽可能短的时间内完成页面的加载,试想一下当你在淘 ...

  5. 2017第八届蓝桥杯C/C++ B组省赛-日期问题

    标题:日期问题 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的 ...

  6. posix信号量与互斥锁

    1.简介 POSIX信号量是一个sem_t 类型的变量,但POSIX 有两种信号量的实现机制:无名信号量和命名信号量.无名信号量可以用在共享内存的情况下, 比如实现进程中各个线程之间的互斥和同步.命名 ...

  7. Unity 3D光源-Point Light点光源详解/灯泡、模拟灯光效果教程

    Unity4大光源之点光源 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  8. Socket远程桌面

    自建Socket转发,使用远程桌面(mstsc)连接家中电脑   网络结构图如下: 开题先放图,一切全靠编哈哈. 进入正题! 如图所示,我们需要一个公网服务器,利用公网服务器将内网的数据进行转发,从而 ...

  9. 【UOJ#22】【UR#1】外星人

    2044年,Picks建成了人类第一台基于量子理论的银河系信息传递机. Picks游遍了宇宙,雇用了 n 个外星人来帮他作为信息传递机的中转站.我们将外星人依次编号为 1 到 n,其中 i 号外星人有 ...

  10. SpringMvc和Mybatis整合总结

    1.先配置mybatis,测试读取数据库 2.自己测试数据 3.配置spring和springmvc, PS:配置web.xml,这样就不用getBean了 4.配置Controller 5.前后台交 ...