任务概述
:今天我们装修【Food产品】页面,其中涉及到模块和模板的使用,相信会对学小程序的大家有所帮助~
下图是今天要做的页面框架
那话不多说 开始装修吧!
一、顶部布局
- 地址
- 商品搜索框
<!-- 顶部布局 -->
<view class="header">
<navigator class="address" url=" " hover-class="none">北京</navigator>
<view class="search">
<navigator url=" ">请输入你想要的内容</navigator>
</view>
</view>
.header{
background: #fff;
padding: 20rpx;
display: flex;
}
.address{
text-align: center;
padding-top: 10rpx;
padding-bottom: 10rpx;
padding-right: 20rpx;
}
.search{
flex: 1;
background: #cccccc;
padding-left:10rpx;
border-radius: 10rpx;
color: #666;
font-size: 28rpx;
padding-top: 10rpx;
padding-bottom: 10rpx;
}
二、导航栏
- 建typeData模块装数据
- 导入模块 调用模块信息
这里最好在外部(pages外)新建一个文件夹装模块,我这里是在外部建了一个“comment”文件夹装。
这里我先放模块的代码,后面详细解析
typeData.js
var list=[{
id:1,
type:0,
imgUrl:"../../images/item1.jpg",
title:"美容养颜"
},{
id:2,
type:1,
imgUrl:"../../images/item2.jpg",
title:"保健调养"
},{
id:3,
type:2,
imgUrl:"../../images/item3.jpg",
title:"补养"
},{
id:4,
type:3,
imgUrl:"../../images/item4.jpg",
title:"减肥"
},{
id:5,
type:4,
imgUrl:"../../images/item5.jpg",
title:"母婴"
},{
id:6,
type:5,
imgUrl:"../../images/item6.jpg",
title:"气节"
},{
id:7,
type:6,
imgUrl:"../../images/item7.jpg",
title:"常见食疗"
},{
id:8,
type:7,
imgUrl:"../../images/item8.jpg",
title:"维生素"
}]
module.exports={
list:list
}
module.exports={
list:list
}
作用:我们上面填完的list列表暴露出去
模块
将复杂的应用程序分解为一系列独立的模块,每个模块负责完成特定的功能或业务逻辑。
可以理解为一个NFC,“滴”一下就能完成“开灯”、“开门”等指定的命令。
这里,我们面对页面中如此多的数据,我们为了实现效果,复制八遍也是能做出来的,但是有没有更加方便且周全的方法呢?
所以 我们就想到创建一个模块来存放我们需要的数据,我们在food页面需要时,只需调用数据即可实现
导入模块
require(“…/…/comment/typedata.js”);
【源码放送】
var myData=require("../../comment/typedata.js");
Page({
data: {
typeData:myData.list,//分类导航数据
}
})
<view class="type">
<view class="item" wx:for="{{typeData}}" wx:key="index">
<navigator url=" ">
<image src="{{item.imgUrl}}" mode=""/>
<view class="info">{{item.title}}</view>
</navigator>
</view>
</view>
三、商品列表布局
- 新建productlist模板,定义每个商品
- 导入模板 调用数据
这里最好在外部(pages外)新建一个文件夹装模板,我这里是在外部建了一个“template”文件夹装。
这里我先放模块的代码,后面详细解析
productlist.wxml
<template name="list">
<view class="list">
<image class="img" src="{{item.pic}}"></image>
<view class="info">
<view class="title">{{item.name}}</view>
<view class="desc">{{item.description}}</view>
<view class="buyName"><text class="red">¥{{item.price}}</text>
{{item.buyName}}</view>
</view>
</view>
</template>
productlist.wxss
/* template/priductlist/priductlist.wxss */
.list{
background: #fff;
padding: 20rpx;
display: flex;
margin-top: 20rpx;
}
.list image{
width: 222rpx;
height: 222rpx;
padding-right: 20rpx;
}
.list .info{
flex: 1;
height: 184rpx;
position: relative;
}
.list .info .title{
font-size: 30rpx;
margin-bottom: 8rpx;
}
.list .info .desc{
color: #666;
font: 26rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.buynum{
font-size: 26rpx;
color: #999;
position: absolute;
left: 0;
bottom: 0;
}
.red{
color: red;
margin-right: 20rpx;
}
模板
template(模板):主要用于显示,简单的说主要是用于嵌入wxml的代码,模板中是可以拥有对应的样式以及逻辑,但是他并没有属于的对应的js文件,它的逻辑依赖于引用的页面。
这里我们用到的template模板,其主要作用是给我们固定了一个框架和样式,我们需要它时,只需导入模块,传递参数即可实现效果。
可以把template模板理解成一个外包公司的产品,在它之前,我们都是自己一条工序走到底,有了它之后,它的精美框架和样式,可以直接被我们使用,不仅方便而且能省去很多的力气,可谓人类之星!
导入模板框架
<import src="../../template/priductlist/priductlist.wxml"/>
<template is="模板名称" data="{{item}}"></template>
导入模板样式
@import '../../template/priductlist/priductlist.wxss';
----放在前面和后面都没有太大影响
【源码放送】
food.wxml
<!-- 商品列表 -->
<import src="../../template/priductlist/priductlist.wxml"/>
<view class="wrapper">
<view wx:for="{{list}}" wx:key="inidex">
<template is="list" data="{{item}}"></template>
</view>
</view>
food.wxss
@import '../../template/priductlist/priductlist.wxss';
/* 列表样式 */
.type{
display: flex;
flex-wrap: wrap;
background-color: #fff;
padding-top: 20rpx;
}
.type .item{
width: 25%;
text-align: center;
color: #666;
font-size: 28rpx;
margin-bottom: 20rpx;
}
.type image{
width: 74rpx;
height: 74rpx;
}
.more{
padding: 20rpx;
color: #666;
text-align: center;
}
food.js
Page({
data: {
location:"北京",
list:[],
},
onLoad(options) {
wx.request({
url: 'http://iwenwiki.com:3002/api/foods/list',
data:{
city:this.data.location,
page:1,
},
success:res=>{
if(res.data.status==200){
this.setData({
list:res.data.data.result
})
}else{
console.log("当前城市无数据")
}
}
})
},
四、全源码
food.wxml
<!--pages/food/food.wxml-->
<!-- 顶部布局 -->
<view class="header">
<navigator class="address" url=" " hover-class="none">北京</navigator>
<view class="search">
<navigator url=" ">请输入你想要的内容</navigator>
</view>
</view>
<!-- 导航栏 -->
<view class="type">
<view class="item" wx:for="{{typeData}}" wx:key="index">
<navigator url=" ">
<image src="{{item.imgUrl}}" mode=""/>
<view class="info">{{item.title}}</view>
</navigator>
</view>
</view>
<!-- 商品列表 -->
<import src="../../template/priductlist/priductlist.wxml"/>
<view class="wrapper">
<view wx:for="{{list}}" wx:key="inidex">
<template is="list" data="{{item}}"></template>
</view>
</view>
food.wxss
/* pages/food/food.wxss */
/* pages/food/food.wxss */
@import '../../template/priductlist/priductlist.wxss';
.header{
background: #fff;
padding: 20rpx;
display: flex;
}
.address{
text-align: center;
padding-top: 10rpx;
padding-bottom: 10rpx;
padding-right: 20rpx;
}
.search{
flex: 1;
background: #cccccc;
padding-left:10rpx;
border-radius: 10rpx;
color: #666;
font-size: 28rpx;
padding-top: 10rpx;
padding-bottom: 10rpx;
}
/* 列表样式 */
.type{
display: flex;
flex-wrap: wrap;
background-color: #fff;
padding-top: 20rpx;
}
.type .item{
width: 25%;
text-align: center;
color: #666;
font-size: 28rpx;
margin-bottom: 20rpx;
}
.type image{
width: 74rpx;
height: 74rpx;
}
.more{
padding: 20rpx;
color: #666;
text-align: center;
}
food.js
// pages/food/food.js
var myData=require("../../comment/typedata.js");
Page({
data: {
location:"北京",
list:[],
typeData:myData.list,//分类导航数据
},
onLoad(options) {
wx.request({
url: 'http://iwenwiki.com:3002/api/foods/list',
data:{
city:this.data.location,
page:1,
},
success:res=>{
if(res.data.status==200){
this.setData({
list:res.data.data.result
})
}else{
console.log("当前城市无数据")
}
}
})
}
})
五、index地址栏传参-补充
昨天运行地址栏传参时,总是出错,今天经过我仔细筛查,总算是给我抓出来了,这里给大家说明一下我的问题所在。
先看我的原代码
因此我断定,我一定是在信息获取时出了问题,我一遍遍检索后,找出了bug
【分析】
在wxml中,通过地址栏传参的参数变量名是id
此时options中的内容应该是id=xxx
而我赋值给id的内容是:options.code
code是哪位?options并没有这个参数
所以因为找不到 所以获取失败
OK 修改完后,果然获取到了res.data的数据
但执行页面依旧空白 这是为啥?
[/左挠头][/右挠头][/思考]
一定是因为页面没有获取到title等信息,我又详细观察了一下,发现,其实我所需要的数据都在第一行里面,也就是我只获取第一行的详细数据,就可以解决了
于是!!!!!美好的一幕出现了
这里放源码,希望可以帮助和我错一个地方的宝汁。
onLoad(options) {
console.log(options)
wx.request({
url: 'http://iwenwiki.com:3002/api/indexlist/detail',
data:{
id:options.id,
},
success:(res)=>{
this.setData({
object:res.data[0]
})
console.log(res.data)
}
})
},
六、结束语
快乐的时间总是短暂的,今天做完了food页面基本的内容,明天做food页面的优化啦!
哈哈有没有感觉今天的文章看着比之前更为舒适啦~
因为本P人今天提前做了准备功课 [/嘻嘻]
写完这些,时间也不早了,要去赶晚课啦
我们明天见~