**重点看本例index.js代码**
**页面目录结构如下:**
![](https://box.kancloud.cn/a6b46678b678946f6527452bbada7505_312x618.png)
**页面渲染效果如下:**
![](https://box.kancloud.cn/a31f5393612dfc4f5615f3049b028790_401x713.png)
## 1.模板页template编写
### 1.1template-star
~~~
<template name="templateStar">
<view class="images">
<block wx:for="{{stars}}" wx:key="index">
<image src="{{item?'/images/icon/star.png':'/images/icon/none-star.png'}}"></image>
</block>
</view>
</template>
~~~
~~~
.images>image{
width: 20rpx;
height: 20rpx;
}
.images{
height: 60rpx;
}
~~~
### 2.1template-movies-item
~~~
<import src="../star/template-star"></import>
<template name="templateMoviesItem">
<view class="movie-item">
<image class="mitem" src="{{imgUrl}}"></image>
<title>{{title}}</title>
<view class="average">
<template is="templateStar" data="{{stars}}"></template>
<text>{{average}}</text>
</view>
</view>
</template>
~~~
~~~
@import "../star/template-star.wxss";
.mitem{
width: 220rpx;
height: 280rpx;
}
.movie-item{
display: flex;
flex-direction: column;
}
.movie-item>title{
font-size: 31rpx;
margin-top: 15rpx;
}
.average{
display: flex;
align-items: center;
}
.average>text{
margin-left: 12rpx;
font-size: 25rpx;
}
~~~
### 1.3template-movies-detail
~~~
<import src="../movies-item/template-movies-item"></import>
<template name="templateMoviesDetail">
<view class="head">
<text>{{title}}</text>
<text class="more">更多> </text>
</view>
<view class="tmovie-itm">
<block wx:for="{{movies}}" wx:key="index">
<template is="templateMoviesItem" data="{{...item}}"></template>
</block>
</view>
<view class="line"></view>
</template>
~~~
~~~
@import "../movies-item/template-movies-item.wxss";
.head{
display: flex;
justify-content: space-between;
padding: 25rpx 20rpx;
font-size: 34rpx;
}
.head .more{
color: aqua;
}
.tmovie-itm{
display: flex;
justify-content: space-around;
}
~~~
## 2.自定义方法util编写
~~~
function http(url, callback, type) {
wx.request({
url,
// header: {}, // 设置请求的 header
header: {
'Content-Type': 'json'
},
success: function (res) {
// success
callback(res, type);
}
})
}
function star(stars) {
var value = stars.slice(0, 1);
var arr = [];
for (let i = 0; i < 5; i++) {
if (i < value) {
arr.push(1);
} else {
arr.push(0);
}
}
return arr;
}
module.exports = {
http: http,
star:star
}
~~~
## 主页面index编写
~~~
//index.js
//获取应用实例
const app = getApp();
const douban = "http://douban.uieee.com/v2/movie/";
import utils from "../../utils/util";
const http = utils.http;
const star = utils.star;
Page({
data:{
in_theaters:{}
},
onLoad() {
var inTheatersurl = douban + "in_theaters" + "?start=0&count=3";
var comingSoonurl = douban + "coming_soon" + "?start=0&count=3";
var top250url = douban + "top250" + "?start=0&count=3";
http(inTheatersurl,this.handleData,"in_theaters");
http(comingSoonurl,this.handleData,"coming_soon");
http(top250url,this.handleData,"top250");
},
handleData(res,type) {
var subjects = res.data.subjects;
var title=res.data.title;
var movies = [];
subjects.forEach(ele => {
var temp = {
imgUrl: ele.images.small,
title: ele.title,
stars: star(ele.rating.stars),
average: ele.rating.average,
}
movies.push(temp)
});
var data={};
/* js给对象赋值属性方式:对象名[属性名]={} */
data[type]={
movies,
type,
title
}
/* console.log(data)
console.log(data.in_theaters) */
/* this.setData({ 数组 }) */
/* this.setData({ 属性 }) */
/* this.setData({ 对象 }) */
/* this.setData(对象内容) */
this.setData(
data
)
}
})
~~~
~~~
<!-- "pages/index/index.js" -->
<import src="../../template/movies-detail/template-movies-detail"></import>
<template is="templateMoviesDetail" data="{{...in_theaters}}"></template>
<template is="templateMoviesDetail" data="{{...coming_soon}}"></template>
<template is="templateMoviesDetail" data="{{...top250}}"></template>
~~~
~~~
@import "../../template/movies-detail/template-movies-detail";
~~~
- 小程序环境配置
- 1.生命周期
- 页面生命周期
- 组件生命周期
- 2.小程序组件
- 点击事件bindtap|catchtap
- swiper轮播
- wx:for循环
- 播放音乐
- map
- tabBar底部页面切换
- scroll-view可滚动视图区域。
- web-view装载显示网页
- priviewImage新页面预览照片
- chooseImage本地选择照片
- onReachBottom上拉刷新,加载等待条
- setStorage缓存
- showToast弹出提示框
- slot父组件wxml传递到子组件
- form表单
- 小程序.wxs,方法在.wxml调用
- 3.组件前身:模板、模板传参
- 4.自定义组件、组件传参|传wxss|wxml代码
- 5.小程序正则
- 6.小程序页面跳转
- 7.open-type 微信开放功能
- 实例
- 1.第一个实例 hello world
- 2.第二个实例豆瓣电影数据渲染
- 豆瓣1.0基本版
- 豆瓣2.0升级版
- 豆瓣3.0豪华版
- 3.第三个实例多接口在同一页面使用
- HTTP封装
- 基础报错提示,类式封装
- Promise回调,类式封装