后浪云百度小程序教程:数据绑定

  • 数据绑定
    • 基础数据绑定
    • 渲染内容
    • 属性绑定
    • 控制属性
    • 运算
      • 对象字面量(对象字面量是三个大括号包裹)

    数据绑定

    SWAN 模板中的动态数据,都从逻辑层 Page 中 data 对象来。

    基础数据绑定

    数据绑定和许多模板引擎一样,数据包裹在双大括号里面。
    双向绑定的数据需包裹在{= =}中。

    例如
    组件 scroll-view 中,scroll-top 和 scroll-left 的使用方法分别为:

    • scroll-top="{= scrollTop =}"
    • scroll-left="{= scrollLeft =}"

    渲染内容

    代码示例

    • SWAN
    • JS
     
     
     
    1. <!-- data-demo.swan -->
    2. <view>
    3. Hello My {{ name }}
    4. </view>
    5. <!-- 被渲染成: Hello My SWAN App -->
     
     
     
    1. // data-demo.js
    2. Page({
    3. data: {
    4. name: 'SWAN App'
    5. }
    6. });

    属性绑定

    代码示例

    • SWAN
    • JS
     
     
     
    1. <!-- attr-demo.swan -->
    2. <view class="c-{{className}}">属性绑定</view>
     
     
     
    1. // attr-demo.js
    2. Page({
    3. data: {
    4. className: 'blue'
    5. }
    6. });

    控制属性

    :属性不需要被双大括号包裹。

    代码示例

    • SWAN
    • JS
     
     
     
    1. <!-- condition-demo.swan -->
    2. <view s-if="flag">如果为flag为true,你看得到我。</view>
     
     
     
    1. // condition-demo.js
    2. Page({
    3. data: {
    4. flag: true
    5. }
    6. });

    运算

    SWAN 模板提供了丰富的表达式类型支持,让使用者在编写视图模板时更方便。

    • 数据访问(普通变量、属性访问)
    • 一元否定
    • 二元运算
    • 二元关系
    • 三元条件
    • 括号
    • 字符串
    • 数值
    • 布尔

    通过下面例子列举支持的表达式类型。

    • SWAN
     
     
     
    1. <!-- operation-demo.swan -->
    2. <!-- 普通变量 -->
    3. <text>{{name}}</text>
    4. <!-- 属性访问 -->
    5. <text>{{person.name}}</text>
    6. <text>{{persons[1]}}</text>
    7. <!-- 一元否定 -->
    8. <text>{{!isOK}}</text>
    9. <text>{{!!isOK}}</text>
    10. <!-- 二元运算 -->
    11. <text>{{num1 + num2}}</text>
    12. <text>{{num1 - num2}}</text>
    13. <text>{{num1 * num2}}</text>
    14. <text>{{num1 / num2}}</text>
    15. <text>{{num1 + num2 * num3}}</text>
    16. <!-- 二元关系 -->
    17. <text>{{num1 > num2}}</text>
    18. <text>{{num1 !== num2}}</text>
    19. <!-- 三元条件 -->
    20. <text>{{num1 > num2 ? num1 : num2}}</text>
    21. <!-- 括号 -->
    22. <text>{{a * (b + c)}}</text>
    23. <!-- 数值 -->
    24. <text>{{num1 + 200}}</text>
    25. <!-- 字符串 + 三元条件 -->
    26. <text>{{item ? ',' + item : ''}}</text>
    27. <!-- 三元运算 -->
    28. <checkbox checked="{{flag ? true : false}}"></checkbox>
    29. <!-- 数组字面量 -->
    30. <text>{{ ['john', 'tony', 'lbj'] }}</text>

    对象字面量(对象字面量是三个大括号包裹)

    :对象字面量支持了在模板里重组对象以及使用扩展运算符...来展开对象。

    代码示例

    • SWAN
    • JS
     
     
     
    1. <!-- template-demo.swan-->
    2. <template name="tag-card">
    3. <view>
    4. <text>标签: {{tag}}</text>
    5. <text>昵称: {{nickname}}</text>
    6. </view>
    7. </template>
    8. <template name="person-card">
    9. <view>
    10. <text>位置: {{pos}}</text>
    11. <text>姓名: {{name}}</text>
    12. </view>
    13. </template>
    14. <template name="team-card">
    15. <view s-for="item, index in teams">
    16. <text>球队: {{index}} - {{item}}</text>
    17. </view>
    18. </template>
    19. <template name="age-card">
    20. <view>
    21. <text>年龄: {{age}}</text>
    22. </view>
    23. </template>
    24. <template is="person-card" data="{{person}}" />
    25. <!-- 对象字面量 -->
    26. <template is="team-card" data="{{ {teams} }}" />
    27. <template is="tag-card" data="{{ {tag, nickname: 'king'} }}" />
    28. <template is="age-card" data="{{ {...person} }}" />
     
     
     
    1. // template-demo.js
    2. Page({
    3. data: {
    4. person: {name: 'Lebron James', pos: 'SF', age: 33},
    5. teams: ['Cleveland Cavaliers', 'Miami Heat', 'Los Angeles Lakers'],
    6. tag: 'basketball'
    7. }
    8. });
    THE END