溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序模板template如何用

發(fā)布時間:2022-04-08 11:00:47 來源:億速云 閱讀:134 作者:iii 欄目:編程語言

本篇內容介紹了“微信小程序模板template如何用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

效果圖

以MUI的實例首頁和列表頁面為實例

微信小程序模板template如何用

微信小程序模板template如何用

通過上圖,可以看出兩個頁面的列表部分很相近,以每行作為單元制作模板。

template模板

1、模板存放的位置以及使用模板頁面存放的位置

微信小程序模板template如何用

template模板的WXML

<!--右側無箭頭 -->
<template name="listNone">
 <view class="tui-menu-list">
 <navigator url="{{item.url}}">
  <block>
  <text>{{item.title}}</text>
  </block>
 </navigator>
 </view>
</template>
<!--右側有箭頭 -->
<template name="list">
 <view class="tui-menu-list tui-youjiantou">
 <navigator url="{{item.url}}">
  <block>
  <text>{{item.title}}</text>
  </block>
 </navigator>
 </view>
</template>

注意:上邊在同一個WXML文件內創(chuàng)建了兩個模板,一個右側有箭頭,一個右側無箭頭,以name名識別。
template模板的WXSS

.tui-menu-list{
 line-height: 80rpx;
 color: #555;
 font-size: 35rpx;
 padding: 0 0rpx 0 10px;
 position: relative;
}

在index頁面使用template模板

WXML

<import src="../../template/list.wxml"/>

<view class="tui-fixed">
 <scroll-view  scroll-y="true">
 <template wx:for="{{menu}}" is="list" data="{{item}}"></template>
 </scroll-view>
</view>
  • 用import 將模板引入;

  • 使用模板,template 的is 屬性和模板中:name 屬性對應,表示你要使用的具體模板,data 屬性是模板中要使用的數(shù)據,注意數(shù)據結構要相同;

  • 可以直接循環(huán)模板,需要也可以在模板外加一層進行循環(huán)。

WXSS

此處將WXSS引入到全局!

@import "./template/list.wxss";
  • 直接使用import 引入列表的WXSS;

  • 此代碼可以放在當前頁面的WXSS(index.wxss)中,也可以放在全局wxss(app.wxss)中 ;

  • 建議:如果項目需要大量使用一個模板,WXSS引入到全局,減少代碼量;如果項目只是很少地方使用模板,可以在需要的頁面引入WXSS。

在list頁面使用template模板

WXML

<import src="../../template/list.wxml"/>

<view class="tui-list-box">
 <view class="tui-list-head">右側無箭頭</view>
 <template wx:for="{{menu}}" is="listNone" data="{{item}}"></template>
</view>
<view class="tui-list-box">
 <view class="tui-list-head">右側有箭頭</view>
 <template wx:for="{{menu}}" is="list" data="{{item}}"></template>
</view>
<view class="tui-list-box-raduis">
 <view class="tui-list-head">圓角列表</view>
 <template wx:for="{{menu}}" is="list" data="{{item}}"></template>
</view>

“微信小程序模板template如何用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI