国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

微信小程序中template模板有什么用

發(fā)布時(shí)間:2021-07-06 14:08 來(lái)源:億速云 閱讀:0 作者:小新 欄目: web開(kāi)發(fā)

這篇文章主要介紹了微信小程序中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í),將立刻刪除涉嫌侵權內容。

97久久精品午夜一区二区| 亚洲AV电影天堂男人的天堂| 欧美猛少妇色XXXXX| 久久青青草原亚洲AV无码APP| 情人伊人久久综合亚洲| 亚洲色成人网一二三区|