在小程序中,import是只引用<template>模板,而import是copy除<template>标签以外的全部代码。
- import
<!-- 引入hello.wxml中name为hello的模板 -->
<import src="../hello/hello.wxml"></import>
<template is="hello"></template>
- include
<!-- include格式 -->
<include src="../hello/hello.wxml"></include>
- 带data的模板引用
<!-- index.wxml -->
//模板引用
<template name="tempItem">
<view>
<view>收件人:{{name}}</view>
<view>电话:{{phoneNum}}</view>
<view>地址:{{address}}</view>
</view>
</template>
<template is="tempItem" data="{{...infor}}"></template>
//index.js
Page({
data: {
infor: {
name: "Etisan",
phoneNum: "13888888888",
address: "China"
}
}
如果引入index.wxml中的<template/>模板,同时会引用index.wxml中的data数据,而不会引用当前页面的data(防止数据错乱)。