Weex使用 mustache 的語法 ({{...}}
) 來對 <template>
中的模板和 <script>
裏的數據進行綁定. 一旦數據額模板綁定了, 數據上的修改會實時的在模板內容中生效。
數據綁定路徑
<template>
<div>
<text style="font-size: {{size}}">{{title}}</text>
</div>
</template>
<script>
module.exports = {
data: {
size: 48,
title: 'Alibaba Weex Team'
}
}
</script>
上面的代碼會把 title
和 size
的數值綁定到模板內容上。
我們也可以通過 .
符號來綁定數據結構中的字段。看一下下面的代碼片段:
<template>
<div>
<text style="font-size: {{title.size}}">{{title.value}}</text>
</div>
</template>
<script>
module.exports = {
data: {
title: {
size: 48,
value: 'Alibaba Weex Team'
}
}
}
</script>
數據綁定表達式
進行數據綁定時,Weex 支持一些簡單的 JavaScript 表達式,例如:
<template>
<div style="flex-direction: row;">
<text>{{firstName + ' ' + lastName}}</text>
</div>
</template>
<script>
module.exports = {
data: {
firstName: 'John',
lastName: 'Smith'
}
}
</script>
這些表達式會在當前的上下文中進行運算。