微信小程序template模板實(shí)例詳解
微信小程序template模板使用
前言
微信小程序中提供了template使用,即相同的板塊可以進(jìn)行代碼互用,如下方的效果圖,就可以用template。

效果圖
一、模板定義
模板最重要的是模板的名稱(chēng),即""
以下是實(shí)例模板代碼
<template name="postItem">
<view class='post-container'>
<view class='post-author-date'>
<image class='post-author' src='{{avatar}}'></image>
<text class='post-date'>{{date}}</text>
</view>
<text class='post-title'>{{title}}</text>
<image class='post-image' src='{{imgSrc}}'></image>
<text class='post-content'>{{content}}</text>
<view class='post-like'>
<image class='post-like-image' src='/images/icon/chat.png'></image>
<text class='post-link-text'>{{collection}}</text>
<image class='post-like-image' src='/images/icon/view.png'></image>
<text class='post-link-text'>{{reading}}</text>
</view>
</view>
</template>
wxss文件
.post-container {
display: flex;
flex-direction: column;
margin-top: 20rpx;
margin-bottom: 40rpx;
background-color: white;
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;
margin-bottom: 5px;
font-size: 26rpx;
}
.post-title {
font-size: 34rpx;
font-weight: 600;
color: #333;
margin-bottom: 10px;
margin-left: 10px;
margin-right: 10px;
}
.post-image {
margin-left: 16px;
width: 100%;
height: 340rpx;
margin: auto 0;
margin-bottom: 15rpx;
}
.post-content {
color: #666;
font-size: 28rpx;
margin-bottom: 20rpx;
margin-left: 20rpx;
margin-right: 20rpx;
letter-spacing: 2rpx;
line-height: 40rpx;
}
.post-like {
font-size: 13px;
flex-direction: row;
line-height: 16px;
margin-left: 16px;
color: gray;
}
.post-like-image {
height: 16px;
width: 16px;
margin-right: 8px;
vertical-align: middle;
}
.post-link-text {
vertical-align: middle;
margin-right: 20px;
}
二、模板使用
引入模板文件
使用模板文件 用is 使用 模板定義時(shí)的名稱(chēng) data里面是循環(huán)里面里面的數(shù)據(jù) 用“...”表示的話(huà),就可以把item里面的數(shù)據(jù)全部平鋪出來(lái),這樣在template里面就不用寫(xiě)“item.xx”了,直接寫(xiě)item里面的屬性就可以了 要使用template的程序wxml文件
<import src="post-item/post-item-template.wxml" />
<view>
<block wx:for="{{postList}}" wx:for-item="item">
<template is="postItem" data="{{...item}}" />
</block>
</view>
wxss 文件
@import 'post-item/post-item-template.wxss';
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
微信小程序 實(shí)現(xiàn)拖拽事件監(jiān)聽(tīng)實(shí)例詳解
這篇文章主要介紹了微信小程序 實(shí)現(xiàn)拖拽事件監(jiān)聽(tīng)實(shí)例詳解的相關(guān)資料,在開(kāi)發(fā)不少應(yīng)用或者軟件都要用到這樣的方法,這里就對(duì)微信小程序?qū)崿F(xiàn)該功能進(jìn)行介紹,需要的朋友可以參考下2016-11-11
js基于div絲滑實(shí)現(xiàn)貝塞爾曲線(xiàn)
這篇文章主要為大家介紹了js基于div絲滑實(shí)現(xiàn)貝塞爾曲線(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)
這篇文章主要介紹了微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)的相關(guān)資料,實(shí)用小技巧自定義輪播圖swiper dots默認(rèn)樣式,需要的朋友可以參考下2017-01-01
使用xml2js庫(kù)進(jìn)行XML數(shù)據(jù)解析
這篇文章主要為大家介紹了使用xml2js庫(kù)進(jìn)行XML數(shù)據(jù)解析用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

