Skip to content

state

基本使用

xml
<!-- 使用状态 -->
<view>{{ count }}</view>
<!-- 更新状态 -->
<button bind:tap="handleTap">+1</button>
js
Page({
  // 定义状态
  data: {
    count: 0,
  },
  handleTap() {
    // 更新状态
    this.setData({ count: this.data.count + 1 })
  },
})

state 双向绑定

在属性前面加上 model:

xml
<view>{{ value }}</view>
<input model:value="{{ value }}" />

<view>{{ checked }}</view>
<checkbox model:checked="{{ checked }}" />
js
Page({
  data: {
    value: 'hello',
    checked: true,
  },
})

注意

属性值只能是一个单一字段的绑定,属性值不能写“属性路径”,也就是说不支持对象和数组;

基于 MIT 许可发布