网站后台不显示,俄罗斯最新军事动态,做网站买域名要买几个后缀最安全,wordpress文章展示页ArkTS基本语法
ArkTS语言简介
ArkTS是鸿蒙生态的应用开发语言。基本语法风格与TypeScript#xff08;简称TS#xff09;相似#xff0c;在TS的生态基础上进一步扩展#xff0c;继承了TS的所有特性#xff0c;是TS的超集。
基本语法概述
扩展能力
基础语法#xff1a…
ArkTS基本语法
ArkTS语言简介
ArkTS是鸿蒙生态的应用开发语言。基本语法风格与TypeScript简称TS相似在TS的生态基础上进一步扩展继承了TS的所有特性是TS的超集。
基本语法概述
扩展能力
基础语法声明式语法组件化机制数据-UI自动关联 Entry
Component
struct Index {State message: string Hello Worldbuild() {Row() {Column() {Text(this.message).fontSize(50).fontWeight(FontWeight.Bold).onClick((){this.message 你好世界})}.width(100%)}.height(100%)}
}状态管理ArkTS提供了多维度的状态管理机制。比如数据传递可以在父子组件之间爷孙组件之间还可以应用全局范围传递或者跨设备传递。同时数据的传递可以只单向传递和可变更的双向传递。
渲染控制条件渲染可根据应用的不同状态渲染对应状态下的UI内容。循环渲染可从数据源中迭代获取数据并在每次迭代过程中创建相应的组件。数据懒加载从数据源中按需迭代数据并在每次迭代过程中创建相应的组件。
装饰器装饰类、结构、方法以及变量。比如 Component自定义组件 Entry自定义组件为入口组件 State组件的状态变量会触发UI刷新。 UI描述声明式方式描述UI
系统组件ArkUI提供的组件 容器组件用来完成页面布局例如 Row、Column 基础组件自带样式和功能的页面元素例如 Text 属性方法设置组建的UI样式
事件方法设置组件的事件回调
声明式UI描述
无参数和有参数组件通过代码可以看得出来组件后面“()”有没有配置内容有就是有参无则是无参。 Column() {//无参数Text(item 1)//有参数Divider()//无参数Text(item 2)
}在有参数的组件配置参数格式如下
image组件的必须参数src Image(https://xyz/test.jpg)应用资源采用$r形式引用 // string类型的参数
Text(test)
// $r形式引入应用资源可应用于多语言场景
Text($r(app.string.title_value))
// 无参数形式
Text()变量或者表达式也可以参数赋值字符串变量嵌套采取${} Image(this.imagePath)
Image(https:// this.imageUrl)
Text(count: ${this.count})配置属性
属性方法以“.”链式调用比如fontSize(30)、width(this.width)、。具体有什么属性根据组件类型来判断属性方法和其他语言的属性大同小异。 Text(hello).fontSize(20).fontColor(Color.Red).fontWeight(FontWeight.Bold)配置事件
事件方法以“.”链式调用的方式配置系统组件支持的事件。 //箭头函数
Button(Click me).onClick(() {this.myText ArkUI;})//匿名函数表达式
Button(add counter).onClick(function(){this.counter 2;}.bind(this))//使用bind以确保函数体中的this指向当前组件//调用函数
myClickHandler(): void {this.counter 2;
}
...
Button(add counter).onClick(this.myClickHandler.bind(this))自定义组件
自定义组件的基本结构 Entry
Component
struct MyComponent {
}struct 自定义组件名 {...}的组合构成自定义组件不能有继承关系
Component仅装饰struct关键字声明的数据结构,一个struct只能被一个Component装饰。
build()函数用于定义自定义组件的声明式UI描述。
Entry装饰该自定义组件为UI页面的入口Entry和UI页面的关系是一对一 tips自定义组件固定格式必有struct、Component、build() 成员函数/变量
成员变量 1.不支持静态成员变量
2.成员变量都是有私有的
3.成员变量的初始化可选可必选 Component
struct MyComponent {private countDownFrom: number 0;private color: Color Color.Blue;build() {}
}Entry
Component
struct ParentComponent {private someColor: Color Color.Pink;build() {Column() {// 创建MyComponent实例并将创建MyComponent成员变量countDownFrom初始化为10将成员变量color初始化为this.someColorMyComponent({ countDownFrom: 10, color: this.someColor })}}
}页面和组件的生命周期和参数传递
生命周期
页面生命周期Entry装饰的组件生命周期
onPageShow页面每次显示时触发。
onPageHide页面每次隐藏时触发一次。
onBackPress:当用户点击返回按钮时触发。
组件生命周期Component装饰的自定义组件的生命周期
aboutToApper组件即将出现时回调该接口;引用组件实例后执行其build函数之前。
aboutToDisapper在自定义组件即将销毁时执行
参数传递
参数传递原则
参数类型和参数声明类型一致不能为null、undefined、undefined或者null的表达式
在函数内部参数值不可修改如要修改可以采取同步回调
参数传递分为按引用传递参数和按值传递 特别注意传递的参数为状态变量时按值传递不能引起UI刷新引用传递可以UI刷新 引用传递函数 CiteData($$: {name: string,age: number}) {}//创建函数{$$.name} //使用参数this.CiteData({ name:this.title,age: 10 }) //使用函数 按值传递函数 ValueData(name: string, age: number){}//创建函数name //使用参数this.ValueData(xxx,10)//使用函数 Component
struct child {State title: string 无名氏build() {Row() {Column() {this.ValueData(this.title,10)//按值传递 调用方式this.CiteData({ name:this.title,age: 10 })// 引用传递 调用方式Button(点击改变值).onClick((){this.title 张三//结果引用传递的name的UI刷新了按值传递的name的UI没变化})}.width(100%)}.height(100%)}Builder CiteData($$: {name: string,age: number}) { //引用传递Text(引用传递${$$.name}的年龄是${$$.age}) //使用方式}Builder ValueData(name: string, age: number) { //按值传递Text(按值传递${name}的年龄是${age}) //使用方式} 文章转载自: http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.rbrd.cn.gov.cn.rbrd.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.tgwfn.cn.gov.cn.tgwfn.cn http://www.morning.jfymz.cn.gov.cn.jfymz.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn