字節(jié)跳動(dòng)小程序開(kāi)發(fā)框架 TTML-引用

2019-07-29 17:28 更新

TTML 提供兩種文件引用方式import和include。

import


import可以在該文件中使用目標(biāo)文件定義的template,如:

在 item.ttml 中定義了一個(gè)叫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 有作用域的概念,即只會(huì) import 目標(biāo)文件中定義的 template,而不會(huì) import 目標(biāo)文件 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 可以將目標(biāo)文件除了 <template/> 外的整個(gè)代碼引入,相當(dāng)于是拷貝到 include 位置,如:

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)