微信小程序 Template詳解及簡(jiǎn)單實(shí)例
微信小程序 Template
模板
WXML提供模板(Template),可以在模板中定義代碼片段,然后在不同的地方使用。可以保證格式以及數(shù)據(jù)的相同。
1-定義模板
使用`<template name="tempName"></template>`標(biāo)簽定義模板,并將模板名稱命名為tempName,賦值給屬性name。在標(biāo)簽內(nèi)部,定義模板結(jié)構(gòu)。如下:
<!-- template.wxml --> <!-- index: int msg: string time: string --> <template name="msgItem"> <view> <text> {{index}}: {{msg}} </text> <text> Time: {{time}} </text> </view> </template>
2-使用模板
使用<template is="tempName" />標(biāo)簽,在需要使用模板的地方。如果要用到j(luò)s文件中的數(shù)據(jù),則需要添加data屬性。如下:
<!-- template.wxml --> <template is="msgItem" data="{{...item}}"/> <template is="msgItem" data="{{...item}}"/> <template is="msgItem" data="{{...item}}"/> 此時(shí)在頁(yè)面上就會(huì)重復(fù)顯示三次相同的信息。 data中的數(shù)據(jù),來(lái)源于js文件,如下: <!-- template.js --> Page({ data: { item: { index: 0, msg: 'this is a template', time: '2016-09-15' } } })
3-is屬性
is屬性不僅可以靜態(tài)的指向渲染的模板,也可以使用Mustache語(yǔ)法,來(lái)動(dòng)態(tài)決定具體需要渲染哪個(gè)模板。如下:
<!-- template.wxml --> // templates <template name="odd"> <view> odd </view> </template> <template name="odd"> <view> even </view> </template> // is屬性使用Mustache語(yǔ)法動(dòng)態(tài)渲染template <block wx:for="{{[1, 2, 3, 4,5]}}"> <template is="{{item % 2 == 0 ? 'even' : 'odd'}}" /> </block>
如上代碼,則會(huì)在頁(yè)面中根據(jù)條件來(lái)顯示odd 或是 even
4-模板的引用
如上都是在同一個(gè)wxml文件中定義和引用模板,而模板的定義和引用是可以分開(kāi)的。即在一個(gè)文件中定義模板,而在其他一個(gè)或多個(gè)文件wxml文件中都可以使用定義好的模板。
從外部文件中引用模板,使用import src="templateUrl" />標(biāo)簽。同樣使用is屬性來(lái)指向要引用的標(biāo)簽。
如目錄如下:
-pages |--index |--index.js |--index.json |--index.wxml |--index.wxss |--template |--template.js |--template.json |--template.wxml |--template.wxss
要在index.wxml中使用template中定義的模板,則需要先在index中利用import來(lái)導(dǎo)入該模板:
<!-- index.wxml --> <import src="../template/template.wxml" <template is="msgItem" data={{...indexData}} // 使用的是自己js文件中的數(shù)據(jù)
要注意import作用域,其僅僅引用目標(biāo)文件中template。如:C import B,B import A,在C中可以使用B定義的template,在B中可以使用A定義的template,但是C不能使用A定義的template。
參考
微信小程序文檔-框架-視圖層-WXML-模板
微信小程序文檔-框架-視圖層-WXML-引用
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 微信小程序 template模板詳解及實(shí)例
- 微信小程序 template模板詳解及實(shí)例代碼
- 微信小程序視圖template模板引用的實(shí)例詳解
- 微信小程序template模板實(shí)例詳解
- 微信小程序-詳解微信登陸、微信支付、模板消息
- 微信小程序 跳轉(zhuǎn)傳遞數(shù)據(jù)的實(shí)例
- 微信小程序使用navigateTo數(shù)據(jù)傳遞的實(shí)例
- 微信小程序 頁(yè)面跳轉(zhuǎn)和數(shù)據(jù)傳遞實(shí)例詳解
- 微信小程序?qū)崿F(xiàn)給嵌套template模板傳遞數(shù)據(jù)的方式總結(jié)
相關(guān)文章
類和原型的設(shè)計(jì)模式之復(fù)制與委托差異
這篇文章主要為大家介紹了類和原型的設(shè)計(jì)模式之復(fù)制與委托差異詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07直觀詳細(xì)的typescript隱式類型轉(zhuǎn)換圖文詳解
這篇文章主要為大家介紹了直觀詳細(xì)的typescript隱式類型轉(zhuǎn)換圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07JavaScript實(shí)現(xiàn)一個(gè)Promise隊(duì)列小工具
這篇文章主要介紹了JavaScript實(shí)現(xiàn)一個(gè)Promise隊(duì)列小工具,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08微信小程序 本地?cái)?shù)據(jù)讀取實(shí)例
這篇文章主要介紹了微信小程序 本地?cái)?shù)據(jù)讀取實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04微信小程序中的onLoad詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了微信小程序中的onLoad詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04微信小程序 picker 組件詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了微信小程序 picker 組件詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01