开源鸿蒙 if/else

  • 2022-08-09
  • 浏览 (636)

if/else

Use if/else for conditional rendering.

icon-note.gif NOTE: - State variables can be used in the if conditional statement.

  • You can use the if conditional statement to implement rendering of child components.

  • The if conditional statement must be used in container components.

  • Some container components limit the type or number of child components. When if is placed in these components, the limitation applies to components created in if and else statements. For example, when if is used in the <Grid> component, whose child components can only be <GridItem>, only the <GridItem> component can be used in the if conditional statement.

Example

Example of using the if conditional statement:

Column() {
    if (this.count > 0) {
        Text('count is positive')
    }
}

Example of using the if, else if, and else conditional statements:

Column() {
    if (this.count < 0) {
        Text('count is negative')
    } else if (this.count % 2 === 0) {
        Divider()
        Text('even')
    } else {
        Divider()
        Text('odd')
    }
}

你可能感兴趣的文章

开源鸿蒙 UI Development

开源鸿蒙 ArkUI Overview

开源鸿蒙 File Organization

开源鸿蒙 app.js

开源鸿蒙 “js” Tag

开源鸿蒙 Lifecycle

开源鸿蒙 Multi-Language Capability

开源鸿蒙 Resource Limitations and Access

开源鸿蒙 CSS

开源鸿蒙 HML

0  赞