当前位置: 首页 > news >正文

Harmony学习(三)

1.模版字符串``

let name:string = '模版字符串'
let age:number = 18
console.log('字符串:',`${name}和${age}`)

2.字符串和数字互相转换

//字符串转数字
let str1:string = '1.1'
console.log('转换',Number(str1))   //output:  1.1
console.log('转换',parseInt(str1)) //output:  1
console.log('转换',parseFloat(str1)) //output: 1.1//数字转字符串
let num1:number = 1
console.log('转换',num1.toString())   //output:  1
console.log('转换',num1.toFixed(2)) //output:  1.00

3.变量

  • 普通变量(不管组件内还是外):只能在初始化时渲染,后续不在刷新
  • 状态变量:需要装饰器@State修饰,改变会引起UI的渲染刷新

4.数组操作

  • 增加:unshift()从开头添加          push()从结尾添加
  • 删除:shift()从开头删        pop()从结尾删   (都会返回删除的项)
  • 任意位置删除或新增:splice(操作的起始位置,删除个数,新增的项1,新增的项2,...)
  • 打印对象数组:JSON.stringify(stuArr)
  • ForEach渲染控制:ForEach(this.titles,(item:string,index) = > {
  • })

5.Grid()  规则的行列布局

Grid(){ForEach([1,2,3,4,5,6,7,8,9,10,11,12],()=>{GridItem(){Column(){}.width('100%').height('100%').backgroundColor(Color.Green).border({width: 1,color: '#666',style: BorderStyle.Solid})}})}.backgroundColor(Color.Pink).width('100%').height(500).columnsTemplate('1fr 1fr 1fr 1fr').rowsTemplate('1fr 1fr 1fr').columnsGap(5).rowsGap(5)

6.badge() 角标

Badge({count:1,position:BadgePosition.RightTop,style:{fontSize:14,badgeSize:20,badgeColor:'#fa2a2d'}}){Column(){}.width('100%').height(200).backgroundColor(Color.Green).border({width: 1,color: '#666',style: BorderStyle.Solid})}

7.遮罩显隐控制:

  • 透明度:opacity:0=>1
  • 层级:zIndex:-1=>99

8.图片动画

  • 缩放scale: 0=>1
  • .animation({duration:500})

9.Swiper() 轮播

Swiper(){Image($r('app.media.ic_gallery_create')).width(200).fillColor(Color.Blue)Image($r('app.media.ic_gallery_create')).width(200).fillColor(Color.Blue)Image($r('app.media.ic_gallery_create')).width(200).fillColor(Color.Blue)}.width('100%').height(100).loop(true).autoPlay(true).interval(4000).vertical(false).indicator(Indicator.dot().itemWidth(20).itemHeight(20).color(Color.Black).selectedColor(Color.Red).selectedItemWidth(50).selectedItemHeight(20))

10.@Extend  扩展组件(样式和事件)

@Extend(组件名)

function  函数名(参数1,参数2) {}

11.@Styles 抽取通用属性、事件

全局:@Styles function setbg() {}  (不支持传参)

组件内: @Styles setbg() {}(不支持传参)

12.@Bulider 自定义构建函数(结构,样式,事件)

@Builder
function navItem(icon: ResourceStr,txt: string) {}

13.Scroll

@Entry
@Component
struct Index {@State message: string = 'Hello World  哈哈哈hahahahah';myScroll:Scroller = new Scroller()build() {Column() {Scroll(this.myScroll){//只支持一个组件Column() {ForEach([1,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3], (item: number, index: number) => {Text('kkkkkkk').width('100%').height(40).fontSize(20).fontWeight(FontWeight.Bold).fontColor('#666').backgroundColor(Color.Pink).margin(5)})}}.width('100%').height(500).scrollable(ScrollDirection.Vertical).scrollBar(BarState.Auto).scrollBarColor(Color.Red).scrollBarWidth(5).edgeEffect(EdgeEffect.Spring).backgroundColor(Color.Yellow).onDidScroll(()=> {const y = this.myScroll.currentOffset().yOffsetconsole.log('滑动距离:', `y:${y}`);})Button('控制滚动条位置').width(200).height(40).margin(20).onClick(() => {this.myScroll.scrollEdge(Edge.Top)})}}
}

14.Tabs  容器组件

@Entry
@Component
struct Index {@State message: string = 'Hello World  哈哈哈hahahahah';@State selectedIndex:number = 0@BuildernavItem(index:number, icon: ResourceStr, selectedIcon:ResourceStr, txt: string) {Column() {Image(index==this.selectedIndex?selectedIcon:icon).width(30)Text(txt).fontSize(20).fontWeight(FontWeight.Bold).fontColor(index==this.selectedIndex?Color.Red:Color.Black)}}build() {Tabs({barPosition:BarPosition.End}){TabContent(){Text('首页')}.tabBar(this.navItem(0,$r('app.media.app_icon'),$r('app.media.app_icon'),'首页'))TabContent(){Text('我的')}.tabBar(this.navItem(1,$r('app.media.startIcon'),$r('app.media.app_icon'),'我的'))}.vertical(false).scrollable(true).animationDuration(0)// .barMode(BarMode.Scrollable)  //滚动.onChange((index:number) => {this.selectedIndex = index})}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Pandas筛选数据的10种方法
  • LVGL - RV1109 LVGL UI刷新效率优化-02
  • 洛谷 P1179 [NOIP2010 普及组] 数字统计 题解
  • Docker 相关命令
  • HarmonyOS应用开发者高级认证,Next版本发布后最新题库 - 单选题序号4
  • 【探索Linux】P.42(传输层 —— TCP面向字节流 | TCP粘包问题 | TCP异常情况 )
  • OpenWrt 为软件包和docker空间扩容
  • 【过滤器 vs 拦截器】SpringBoot中过滤器与拦截器:明智选择的艺术(如何在项目中做出明智选择)
  • html+css 实现水波纹按钮
  • TCP/IP协议(全的一b)应用层,数据链层,传输层,网络层,以及面试题
  • 披荆斩棘:Python开发者在市场低迷期快速找到工作的策略
  • C# 植物大战僵尸
  • 关于海康网络摄像头使用VLC或者Opencv拉流有延迟的问题
  • Linux网络:传输层TCP协议(四)拥塞控制及延迟应答
  • 前端和Postman调用同一个接口,拿到的数据不一样
  • 【译】JS基础算法脚本:字符串结尾
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • 2019年如何成为全栈工程师?
  • 4. 路由到控制器 - Laravel从零开始教程
  • crontab执行失败的多种原因
  • HTML-表单
  • miaov-React 最佳入门
  • Python实现BT种子转化为磁力链接【实战】
  • vue-router 实现分析
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 程序员最讨厌的9句话,你可有补充?
  • 缓存与缓冲
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 力扣(LeetCode)357
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 如何在GitHub上创建个人博客
  • 深度学习中的信息论知识详解
  • 使用SAX解析XML
  • 微服务框架lagom
  • (12)目标检测_SSD基于pytorch搭建代码
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (zt)最盛行的警世狂言(爆笑)
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (附源码)ssm码农论坛 毕业设计 231126
  • (十六)视图变换 正交投影 透视投影
  • (算法)N皇后问题
  • (一一四)第九章编程练习
  • (转)甲方乙方——赵民谈找工作
  • (转载)虚函数剖析
  • (自适应手机端)响应式服装服饰外贸企业网站模板
  • ***检测工具之RKHunter AIDE
  • .bat批处理(一):@echo off
  • .JPG图片,各种压缩率下的文件尺寸
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .net6+aspose.words导出word并转pdf
  • .Net8 Blazor 尝鲜
  • .NET建议使用的大小写命名原则
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验