微信的模版主要是用于公共界面管理,比如彈窗或公共頁面都可以用模版定義。
一、模板創(chuàng)建
1、首先在pages文件夾中新建一個template文件夾,文件夾中新建一個template.wxml文件
<!--template.wxml-->
<template name="msgItem">
<view>
<text class="info">This is template.wxml文件,我是一個模板</text>
</view>
</template>
2、接下來我們就給模板增加樣式文件,在pages/template文件夾中新建一個template.wxss文件,然后對模板文件,添加一個簡單樣式。
/* pages/template/template.wxss */
.template_style{
font-size: 30rpx;
color: #000000;
}
二、模板使用
1、在我們要使用的wxml加載
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<!--使用-->
<template is="msgItem"/>
2、當前頁面樣式導入
/**index.wxss**/
@import "../template/template.wxss";
注意:
(1)index.wxml中template 標簽的is屬性與template.wxml中template 標簽的name屬性值相同
(2)index.wxml文件中要通過import標簽聲明需要使用的模板文件
三:數(shù)據傳遞
1、有時候模版需要外面給他傳遞數(shù)據顯示,這時先定義參數(shù)
<!--template.wxml-->
<template name="msgItem">
<view>
<text class="info">{{infoData}}</text>
</view>
</template>
2、接下來我們在index.wxml中傳遞模板中所需要的參數(shù),修改后的代碼如下:
<!--index.wxml-->
<!-- 聲明需要使用的模板文件 -->
<import src ="../template/template.wxml"/>
<view>This is index.wxml</view>
<!--使用-->
<template is="msgItem" data="{{infoData: '這是一個參數(shù)'}}"/>
作者:木馬不在轉
鏈接:http://www.itdecent.cn/p/cfca91e9a78b