引用
MXML 提供两种文件引用方式 import
和 include
。
import
import
可以在该文件中使用目标文件定义的 template,如:
在 item.mxml 中定义了一个叫 item 的 template:
<!-- modal.mxml -->
<template name="modal">
<text>{{msg}}</text>
</template>
在 index.MXML 中引用了 item.mxml ,就可以使用 item 模板:
<import src="modal.mxml "/>
<template is="modal" data="{{msg: 'hello cortana'}}"/>
import 的作用域
import
有作用域的概念,即只会 import
目标文件中定义的 template,而不会 import
目标文件 import
的 template。
如:C import
B,B import
A,在 C 中可以使用 B 定义的 template,在 B 中可以使用 A 定义的 template,但是 C 不能使用 A 定义的 template。
<!-- A.mxml -->
<template name="A">
<text> A template </text>
</template>
<!-- B.mxml -->
<import src="a.mxml"/>
<template name="B">
<text> B template </text>
</template>
<!-- C.mxml -->
<import src="b.mxml"/>
<template is="A"/> <!-- Error! Can not use tempalte when not import A. -->
<template is="B"/>
include
include 可以将目标文件除了 <template/>
<js/>
外的整个代码引入,相当于是拷贝到 include 位置,如:
<!-- index.mxml -->
<include src="header.mxml"/>
<view> body </view>
<include src="footer.mxml"/>
<!-- header.mxml -->
<view> header </view>
<!-- footer.mxml -->
<view> footer </view>