模板template的使用
页面结构是完全一样的,适合使用模板来完成页面搭建,就不用写那些重复的代码了,而且修改界面的时候,也只需要稍微改动模板
一、定义模板
1、新建一个template文件夹用来管理项目中所有的模板;
2、新建一个person.wxml文件来定义模板;
3、使用name属性,作为模板的名字。然后在内定义代码片段。
person.wxml
<template name="person">
<view class="courses-list" style='background-image: url("{{cardImage}}")'>
<view class="money-border">
<text class="money">{{cardMoney}}</text>/课时
</view>
<view class="mark">
<view>{{cardMark}}</view>
</view>
</view>
</template>
person.wxss
.courses-list {
height: 342rpx;
margin: 40rpx auto;
border-top: 2rpx solid #f0f0f0;
margin-top: 10rpx;
flex-direction: column;
align-items: flex-start;
width: 670rpx;
border-radius: 8px;
}
.money-border {
font-family: PingFangSC-Semibold;
font-size: 28rpx;
color: #fff;
letter-spacing: -0.41px;
height: 56rpx;
margin-top: 20rpx;
margin-left: 20rpx;
}
.money {
font-size: 40rpx;
}
.name {
margin-top: 158rpx;
margin-left: 20rpx;
font-family: PingFangSC-Semibold;
color: #fff;
letter-spacing: -0.41px;
}
.ename {
font-size: 40rpx;
}
.cname {
font-size: 30rpx;
}
.mark {
font-family: PingFangSC-Regular;
font-size: 28rpx;
color: #fff;
margin-left: 20rpx;
letter-spacing: -0.41px;
margin-bottom: 18rpx;
}
在页面引入样式文件和视图文件
Course.wxss
@import "../template/person.wxss";
Course.wxml
<import src="../template/person.wxml" />
<block wx:for="{{list}}" wx:key="idx">
<template is="person" data="{{...item}}"></template>
</block>
<view class="tab-list" wx:for="{{list}}" wx:key="index">
<template is="day-tab" data="{{...item,index:index,currentTarget:currentTarget}}" wx:key="index"></template>
</view>
传数据时item前面加三个点... 模板里面就不需要写item了,
传多个数据到模板用逗号分开,item 是对象,index是单个数据,要用键值对