WeChat Mini Program Quote


Reference

WXML provides two file reference methods import and include.

import

importYou can use the template defined by the target file in this file, such as:

in item.wxml A template called item is defined in:

<!-- item.wxml -->
<template name="item">
  <text>{{text}}</text>
</template>

references item.wxml in index.wxml, you can use the item template :

<import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/>

The scope of import


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 import B, B import A, in C you can use the template defined by B, and in B you can use the template defined by A , but C cannot use the template defined by A.

<!-- A.wxml -->
<template name="A">
  <text> A template </text>
</template>
<!-- B.wxml -->
<import src="A.wxml"/>
<template name="B">
  <text> B template </text>
</template>
<!-- C.wxml -->
<import src="B.wxml"/>
<template is="A"/>  <!-- Error! Can not use tempalte when not import A. -->
<template is="B"/>

include

includeYou can introduce the entire code of the target file except <template/>, which is equivalent to copying to include position, such as:

<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>
<!-- header.wxml -->
<view> header </view>
<!-- footer.wxml -->
<view> footer </view>