条件渲染

WePY 推荐使用 v-showv-if 控制条件渲染


  • wx:if
  1. // example 01
  2. <div wx:if="{{ condition }}"></div>
  3. // example 02
  4. <div wx:if="{{ condition2 }}"></div>
  5. <div wx:else></div>
  6. // example 03
  7. <div wx:if="{{ condition3 }}"></div>
  8. <div wx:elif="{{ condition4 }}"></div>
  9. <div wx:else></div>
  • hidden
  1. <div hidden="{{ condition }}"></div>

Vue 绑定语法


  • v-if
  1. // example 01
  2. <div v-if="condition"></div>
  3. // example 02
  4. <div v-if="condition2"></div>
  5. <div v-else></div>
  6. // example 03
  7. <div v-if="condition3"></div>
  8. <div v-else-if="condition4"></div>
  9. <div v-else></div>

编译后:

  1. // example 01
  2. <view wx:if="{{ condition }}"></view>
  3. // example 02
  4. <view wx:if="{{ condition2 }}"></view>
  5. <view wx:else></view>
  6. // example 03
  7. <view wx:if="{{ condition3 }}"></view>
  8. <view wx:elif="{{ condition4 }}"></view>
  9. <view wx:else></view>
  • v-show
  1. <div v-show="condition"></div>

编译后:

  1. <view hidden="{{ !condition }}"></view>