- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) >
- 微信小程序中template模板有什么用
這篇文章主要介紹了微信小程序中template模板有什么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
微信小程序 template模板詳解
如下圖,我在做華企商學(xué)院小程序的時(shí)候,課程搜索結果頁(yè)和課程列表頁(yè)結構是完全一樣的,這時(shí)就非常適合使用模板來(lái)完成頁(yè)面搭建。實(shí)現一次定義,到處使用。
模板
一、定義模板
1、新建一個(gè)template文件夾用來(lái)管理項目中所有的模板;
2、新建一個(gè)courseList.wxml文件來(lái)定義模板;
3、使用name屬性,作為模板的名字。然后在<template/>內定義代碼片段。
注意:
a.可以看到一個(gè).wxml文件中可以定義多個(gè)模板,只需要通過(guò)name來(lái)區分;
b.模板中的數據都是展開(kāi)之后的屬性。
<template name="courseLeft"> <navigator url="../play/play?courseUuid={{courseUuid}}&isCompany={{isCompany}}"> <view class="item mr26"> <image src="{{imagePath}}" mode="aspectFill"></image> <view class="course-title"> <text class="title">{{courseName}}</text> <text class="author">- {{teacherName}}</text> </view> <view class="course-info clearfix"> <view class="fl"><text class="play">{{playCount}}</text></view> <view class="fr"><text class="grade">{{score}}</text></view> </view> <view wx:if="{{studyProgress}}" class="tip-completed">{{studyProgress}}</view> </view> </navigator> </template> <template name="courseRight"> <navigator url="../play/play?courseUuid={{courseUuid}}&isCompany={{isCompany}}"> <view class="item"> <image src="{{imagePath}}" mode="aspectFill"></image> <view class="course-title"> <text class="title">{{courseName}}</text> <text class="author">- {{teacherName}}</text> </view> <view class="course-info clearfix"> <text class="play fl">{{playCount}}</text> <text class="grade fr">{{score}}</text> </view> <view wx:if="{{studyProgress}}" class="tip-completed">{{studyProgress}}</view> </view> </navigator> </template>
二、使用模板
1、使用 is 屬性,聲明需要的使用的模板
<import src="../../templates/courseList.wxml"/>
2、將模板所需要的 data 傳入,一般我們都會(huì )使用列表渲染。
<block wx:for="{{courseList}}"> <template is="{{index%2 === 0 ? 'courseLeft' : 'courseRight'}}" data="{{...item}}"></template> </block>
注意:
a.可以通過(guò)表達式來(lái)確定使用哪個(gè)模板is="{{index%2 === 0 ? 'courseLeft' : 'courseRight'}}"
或者通過(guò)wx:if來(lái)確定。index是數組當前項的下標。
<template wx:if="{{index%2 === 0}}" is="courseLeft" data="{{...item}}"></template> <template wx:else is="courseRight" data="{{...item}}"></template>
b. data 是要模板渲染的數據,data="{{...item}}" 寫(xiě)法是ES6的寫(xiě)法,item是wx:for當前項,... 是展開(kāi)運算符,在模板中不需要再{{item.courseName}} 而是直接{{courseName}} 。
三、模板樣式
1、在新建模板的時(shí)候同時(shí)新建一個(gè)courseList.wxss 的文件,與CSS同樣的寫(xiě)法控制樣式。
2、在需要使用模板的頁(yè)面 .wxss文件中import進(jìn)來(lái);或者直接在app.wxss中引入,這樣只需要一次引入,其他文件就不用引入了。
@import url("../template/courseList.wxss");
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2 建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站