【template、import、include】微信小程序:模板(template)、引用(import、include)说明

时间:2023-03-09 08:06:53
【template、import、include】微信小程序:模板(template)、引用(import、include)说明

模板(template):

定义模板

<template name="[String]">
<!-- 模板代码 -->
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>

属性 name 必须,命名该模板。

使用模板

 <template is="msgItem" data="{{...item}}"/>
 Page({
data: {
item: {
index: 0,
msg: 'this is a template',
time: '2016-09-15'
}
}
})

属性:

is 【必须】 指明要使用的模板名称,可以是Mustache语法生成的名称。

data 【可选】用于设置模板所需要的数据,模板定义中可以直接使用该数据

import

import可以在该文件中使用目标文件定义的template,语法:

 <import src="[文件路径]"/>

import只能获取目标文件中的template部份,并且不会获取目标文件中import中的模板。

include

include 可以将目标文件除了 <template/> <wxs/> 外的整个代码引入,相当于是拷贝到 include 位置,语法:

 <include src="[文件路径]"/>