Reference
WXML provides two file reference methods: import and include.
import
Import can use the template defined in the target file in the file, such as: a template called item is defined in item.wxml.
js
<!-- item.wxml -->
<template name="item">
<text>{{text}}</text>
</template>In index.wxml, item.wxml is referenced, and the item template can be used:
js
<import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/>Import scope
Import has the concept of scope, that is, it will only import the template defined in the target file, but not the template imported by the target file.
For example: C imports B, B imports A, C can use the template defined by B, B can use the template defined by A, but C cannot use the template defined by A.
js
<!-- A.wxml -->
<template name="A">
<text> A template </text>
</template>js
<!-- B.wxml -->
<import src="a.wxml"/>
<template name="B">
<text> B template </text>
</template>js
<!-- C.wxml -->
<import src="b.wxml"/>
<template is="A"/> <!-- Error! Can not use tempalte when not import A. -->
<template is="B"/>include
Include can import the entire code of the target file except <template/> <wxs/>, which is equivalent to copying it to the include location, such as:
js
<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>js
<!-- header.wxml -->
<view> header </view>js
<!-- footer.wxml -->
<view> footer </view>