前面写了几篇文章,简单地介绍了一下小程序。相信完整看下来的读者,对微信小程序应该有了必定的认识。学习,须要边学边练,这样掌握起来快,反正我喜欢这么去学习同样新的技术。学而不思则罔,思而不学则殆嘛。下面,咱们一块儿从0开始,来作一个简单的实例。这个实例我分红2篇文章来说解:1,完成界面、API交互 2,问题总结及注意事项。php
例子描述:咱们一块儿来作一个叫作“知乎新闻”的小程序,小程序经过zhihu的API查询新闻,把最新的新闻标题列出来,点击标题后显示新闻的详细内容。
一、首页html
设计思路:
⑴ 头部“知乎新闻”,经过设置app.json里的window属性就能够了。
⑵ 底部的“首页”、“主题新闻”,经过设置app.json里的tabBar属性就能够了。
⑶ 图片滚动新闻,用swiper滑块滚动视图组件。
⑷ 新闻列表,用view组件;其中,有一个点击新闻标题后,显示新闻详细内容的功能。这个功能,用navigator组件。json
代码编写:
⑴ app.json小程序
"window":{ "backgroundTextStyle":"dark", "navigationBarBackgroundColor": "#00a2ea", "navigationBarTitleText": "知乎新闻", "navigationBarTextStyle": "white" }, "tabBar": { "color": "#353535", "selectedColor": "#3cc51f", "borderStyle": "white", "backgroundColor": "#ffffff", "list": [{ "pagePath": "pages/index/index", "iconPath": "images/icon_API.png", "selectedIconPath": "images/icon_API_HL.png", "text": "主页" }, { "pagePath": "pages/theme/theme", "iconPath": "images/icon_component.png", "selectedIconPath": "images/icon_component_HL.png", "text": "主题新闻" }] },
跟pages同级建立一个images目录,把png图片文件拷贝到这个目录。而后点击开发者工具左侧的“编译”,显示以下界面:微信小程序
⑵ 添加“图片滚动新闻”
① index.wxml里,添加以下代码:api
<view> <swiper indicator-dots="true" autoplay="true" interval="3000" duration="2000"> <swiper-item > <image src="" data-id="" /> <text>title</text> </swiper-item> </swiper> </view>
如今,界面有了,但swiper组件里没有图片来源,下面须要经过调用zhihu的API,把图片来源动态地获取出来。微信
② index.js里,添加以下代码:app
data: { banner: [], duration: 2000, // 滑动动画时长 indicatorDots: true, // 是否显示面板指示点 autoplay: true, // 是否自动切换 interval: 3000 // 自动切换时间间隔 }, onLoad () { var that = this wx.request({ //调用API,获取新闻数据 url: 'http://news-at.zhihu.com/api/4/news/latest', headers: { 'Content-Type': 'application/json' }, success (res) { that.setData({ banner: res.data.top_stories }) } }) },
③ 这里须要说明下,调用任何API前,须要先了解API返回的数据格式。
http://news-at.zhihu.com/api/...返回的数据格式以下:框架
为了在swiper中动态显示图片,标题。index.wxml的代码须要修改为:工具
<view> <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}"> <block wx:for="{{banner}}"> <swiper-item > <image src="{{item.image}}" data-id="{{item.id}}" /> <text>{{item.title}}</text> </swiper-item> </block> </swiper> </view>
编译后,显示的界面以下:
④ 点击滚动图片,显示新闻具体内容。
<image>组件里添加bindtap属性,修改后的代码以下:
<image src="{{item.image}}" data-id="{{item.id}}" bindtap="bindViewTap" class="banner-image"/>
⑤ index.js须要添加相应的bindViewTap方法,以下:
bindViewTap(e) { wx.navigateTo({ url: '../detail/detail?id=' + e.target.dataset.id }) },
二、detail页面
前面的首页,点击滚动图片,须要显示新闻具体内容。这时,就须要建立1个detail页面,页面设计以下:
⑴ 跟index目录并级,建立detail目录,并建立detail.wxml, detail.js 2个文件。
① detail.js的代码以下:
onLoad (options) { var that = this wx.request({ url: 'http://news-at.zhihu.com/api/4/news/' + options.id, headers: { 'Content-Type': 'application/json' }, success (res) { that.setData({ art: res.data }) } }) }
② wxml的代码以下:
<view> <view> <image src="{{art.image}}"/> <view>{{art.title}}</view> <view>{{art.image_source}}</view> </view> <view> {{art.body}} </view> </view>
编译后,显示的界面以下:
③ 发现小程序对含html格式的数据,显示有问题。目前,只能人工把html代码过滤掉。后期,我但愿腾讯能推出html组件,这样用户在<html> </html>里显示就没问题了。
固然,如今你能够本身写js代码,把html格式的数据处理掉。
三、完善首页的“新闻列表”
⑴ 用wx:for循环列出新闻,用navigator页面连接组件显示每一条新闻。
⑵ 添加“更多”按钮
具体代码,我这里就不列了。由于再列代码,这文章也忒长了吧。我喜欢简单,简单。写到这里,我以为用写文章的方式来说实例,效果不是很好。下面我会录个视频教程,手把手教你写完这个实例。
做者名字:有渔
有渔系列文章:
有渔微信小程序系统概述《六》小程序的API
有渔微信小程序技术分析《五》Mustache语法要点总结
有渔微信小程序系统进阶《四》小程序组件
有渔微信小程序系统概述《三》view层及小程序框架概述
有渔微信小程序系统概述《二》了解.js文件及.json文件
有渔微信小程序系统概述《一》认识微信小程序与HelloWorld