关于微信小程序的引用功能,WXML提供两种文件引用方式:import和include。import和include两者的区别在于import引用模板文件,而include引用整个除了<template/>的文件。
一、import引用
首先需要在page当中建立一个item文件,如下图:
然后在item.wxml文件内输入以下代码:
<!-- include引用的内容 -->
<text>include引用了这是模板之外的内容</text>
<!-- import引用的内容 -->
<template name="item">
<text>{{text}}</text>
</template>
其次再在index.wxml文件内进行引用,代码如下:
<import src="../item/item.wxml"/>
<template is="item" data="{{text:'import引用了这是模板之内的内容'}}"/>
最有进行编译,由于在index.wxml文件内采用的是import引用,故引用的是template模板,结果如下:
二、include引用
在import引用的基础上进行改变一下就得到了include引用。item.wxml文件内代码不变,在index.wxml内进行改变即可,代码如下:
<include src="../item/item.wxml"/>
<template is="item" data="{{text:'import引用了这是模板之内的内容'}}"/>
编译后的结果如下: