一个WXML可以通过import或include引入其他WXML文件,两种方式都能引入WXML文件,区别如下:
1. import引入WXML文件后只接受模板的定义,忽略模板之外的所有内容,而且使用过程中有作用域的概念。
2. include引入WXML文件后只接受模板之外的定义,即引入文件中除模板以外的代码直接拷贝到<include/>位置。
3. import是引入模板定义,include是引入组件。
import的使用
1. 示例
<!--pages/static/four/four.wxml-->
<template name="myTemplate">
<view>模板中的内容</view>
<view>{{content}}</view>
</template>
<template name="myTemplate2">
<view>嵌套使用的模板</view>
</template>
<template is="myTemplate2" data="{{content}}">
</template>
<view bindtap='tap1'>
触发1{{tap1}}
<view bindtap='tap2'>
触发2{{tap2}}
<view bindtap='tap3'>
触发3{{tap3}}
</view>
</view>
</view>
<!--pages/static/five/five.wxml-->
<import src='/pages/static/four/four.wxml' />
<template is='myTemplate' data="{{content}}"></template>
<include src='/pages/static/four/four.wxml' />
注释:
src:该属性是需要被引入文件的相对地址。
变量:需要重新定义和赋值