字节跳动小程序开发框架 TTML-引用

2019-07-29 17:28 更新

TTML 提供两种文件引用方式import和include。

import


import可以在该文件中使用目标文件定义的template,如:

在 item.ttml 中定义了一个叫item的template:

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

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

<!-- index.ttml -->
<import src="item.ttml"/>
<template is="item" data="{{text: 'forbar'}}"/>

import 的作用域


import 有作用域的概念,即只会 import 目标文件中定义的 template,而不会 import 目标文件 import 的 template。

如:C import B,B import A,在C中可以使用B定义的template,在B中可以使用A定义的template,但是C不能使用A定义的template。

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

include


include 可以将目标文件除了 <template/> 外的整个代码引入,相当于是拷贝到 include 位置,如:

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


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号