ArkTs核心语法

if分支语句

复制代码
let name:string='人'
if(name=='人'){
  console.log('name是人不是狗');
}else if(name=='狗'){
  console.log('name是人不是狗');
}else {
  console.log('name不是人');
}

条件表达式

复制代码
条件?成立表达式1:不成立表达式2

let num1:number=1
let num2:number=2
let flag = (num1<num2 ? 'num1<num2' : 'num1num2')
console.log('flag--',flag);

条件渲染

根据逻辑条件结果不同,渲染不用的UI内容

复制代码
let name:string='人'

@Entry
@Component
struct Index{
  build() {
   Column(){
     if(name=='人'){
       Text('name是人不是狗')
     }else if(name=='狗'){
       Text('name是人不是狗')
     }else {
       Text('name不是人')
     }
   }
  }
}

循环渲染

根据数组数据重复渲染UI内容

ForEach(数组,(item:类型,index:number)=>{})

复制代码
let names:string[]=['张三','李四','王五']

@Entry
@Component
struct Index{
  build() {
   Column(){
     ForEach(names,(item:string,index:number)=>{
       Text(item+index)
     })
   }
  }
}

状态管理

应用的运行时的状态参数,当参数改变时,UI渲染刷新

状态变量:用装饰器修饰的变量,使用变量的地方会刷新

复制代码
@ComponentV2

状态值@Local修饰

组件关联状态值 .onClick(()=>{this.num-- })

使用状态值要用this.调用

复制代码
@Entry
@ComponentV2
struct Index{
  @Local num:number=1;

  build() {
    Column(){
      Row(){
        Text('-')
          .width(40)
          .height(40)
          .border({width:1,color:'#999',radius:{topLeft:3,bottomLeft:3}})
          .textAlign(TextAlign.Center)
          .onClick(()=>{
            if (this.num>1) {
              this.num--
            }
          })
        Text(this.num.toString())
          .width(40)
          .height(40)
          .border({width:1,color:'#999'})
          .textAlign(TextAlign.Center)
        Text('+')
          .width(40)
          .height(40)
          .border({width:1,color:'#999',radius:{topRight:3,bottomRight:3}})
          .textAlign(TextAlign.Center)
          .onClick(()=>{
            this.num++
          })
      }

    }
  }
}

@Buider自定义构建函数

用于封装需要重复使用的UI元素,提升复用性

相关推荐
用户059540174462 小时前
LLM对话记忆测试踩坑实录:手工回归30分钟,自动化后2分钟发现3个隐藏Bug
前端·css
Ashley的成长之路3 小时前
前端性能优化实战手册·第2篇:资源加载策略全解
前端·性能优化·资源加载·http/3·性能优化实战·资源加载优化
浅水壁虎3 小时前
vue基础(第二章 )
前端·javascript·vue.js
界面开发小八哥3 小时前
界面控件DevExtreme v26.1新版亮点——支持Angular 22
前端·javascript·angular.js·devexpress·ui开发·devextreme
Getflare3 小时前
前端 + UI 设计 + AI:这不是三个工种,是一个新三角能力模型(附自检清单)
前端·人工智能·ui
oil欧哟3 小时前
我做了一个 Vibe Coding 术语学习站:VibeHub
前端·ai·agent·独立开发·vibe coding
审小匠OpenCPAi4 小时前
银行流水核查怎么自动化?单边匹配、双向勾稽与图聚类异常检测的工程对比
java·前端·人工智能·python·审计
Revolution614 小时前
一个公共表格组件,是怎么一步步失控的
前端·前端工程化
mONESY4 小时前
React useState 核心原理与最佳实践:从异步更新到性能优化全解
javascript