# 引用

快应用 QXML 文件引用方式有 importinclude 两种。

# import

import 一个目标文件后,可以在当前文件中使用目标文件中定义的 template 。如:

在 template.qxml 中定义了一个叫 itemtemplate

<!-- template.qxml -->
<template name="item">
  <text>{{text}}</text>
</template>
1
2
3
4

在 index.qxml 中引用了 template.qxml 后,就可以使用 item 模板:

<import src="template.qxml" />
<template is="item" data="{{text: 'Hello QuickApp'}}" />
1
2

# import 的作用域

import 只会引入直接目标文件的 template, 而不会引入目标文件 import 的 template。

比如 C import B,B import A,那么:

  • 在 B 中可以使用 A 定义的template
  • 在 C 中可以使用 B 定义的 template, 但不能使用 A 定义的 template
<!-- A.qxml -->
<template name="A">
  <text> A template </text>
</template>
1
2
3
4
<!-- B.qxml -->
<import src="a.qxml" />
<template name="B">
  <text> B template </text>
</template>
1
2
3
4
5
<!-- C.qxml -->
<import src="b.qxml" />
<template is="A" />
<!-- 无法使用 A -->
<template is="B" />
1
2
3
4
5

# include

include 的作用相当于代码拷贝。可以将目标文件除了 <template/> <qjs/> 外的代码复制到 include 位置,如:

<!-- index.qxml -->
<include src="header.qxml" />
<view> body </view>
<include src="footer.qxml" />
1
2
3
4
<!-- header.qxml -->
<view> header </view>
1
2
<!-- footer.qxml -->
<view> footer </view>
1
2

在线客服