【Harmony】鸿蒙开发经验

1.下拉刷新不支持边缘滑动效果

问题描述

页面下拉刷新无响应,拉不动

原因

The edgeEffect value must be EdgeEffect.None
[图片]
[图片]
文档链接:https://ohpm.openharmony.cn/#/cn/detail/@ohos%2Fpulltorefresh
另外关联的scroller也需要设置

2.lottie name不能相同

问题描述

从首页跳转到租约页,首页loading消失前,切换到租约页,租约页loading会提前消失。

原因

[图片]
KeLoadingDialog传入Lottie的name是写死的,从A页面切换到B页面时,A的lottie destroy会导致B的lottie也destroy,提前消失了
解决方案
lottie name随机

3.HTTPS图片连代理加载不出来

装证书或不连代理

4.CustomDialog作为临时变量内部无法关闭

问题描述

CustomDialogController作为临时变量,在弹窗内部无法关闭,controller为undefined
[图片]

解决方案

dialog作为组件的成员变量
private dialogController: CustomDialogController | null = null;
[图片]
推荐不依赖UI组件的弹窗方式,类似RPLoadingUtil
[图片]
[图片]

5.类型转换不能使用as

问题描述

接口下发的某字段text有时是number类型,有时是string类型
某个方法的入参是string类型,用 text as string 断言为string
运行时 number 会报TypeError crash

原因

[图片]
解决方案
String(text)

6.关闭H5白名单校验

问题描述

未加过白名单的链接无法打开

解决方案

摇一摇-调试选项-取消使用Web白名单
[图片]

7.List组件不设置高度滑动不到底

解决方案

layoutWeight(1)可以自适应占满剩余空间父容器尺寸

8.merge分支后changeid为空

git merge –no-f dev-1215 自动生成一个changeid

9.切换分支后清理缓存

ohpm clean
sh lj-clean
ohpm install

10.字号、色值使用常量类

好处:

  • 色值:主题色统一修改
  • 字号:字号统一修改单位

    11.真机调试

    需要登录账号 自动签名:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-signing-V5#section18815157237

    12.环境配置

    问题描述

    ERR_PNPM_FETCH_404
    Installing dependencies…
     ERR_PNPM_FETCH_404  GET https://repo.huaweicloud.com/repository/npm/@ke%2Frouter-plugin: Not Found - 404
    This error happened while installing a direct dependency of /Users/lichang/.hvigor/project_caches/b8db3eae16304d1d8731239702645cb2/workspace
    @ke/router-plugin is not in the npm registry, or you have no permission to fetch it.
    No authorization header was set for the request.
    Error: /Users/lichang/.hvigor/wrapper/tools/node_modules/.bin/pnpm install execute failed.See above for details.

    解决方案

    配置npm
    将下面配置添加到~/.npmrc中,邮箱地址替换为自己的邮箱,//部分保持原样,不要删除
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @ke:registry=http://artifactory.intra.ke.com/artifactory/api/npm/npm-mobile-release/
    @ohos:registry=https://repo.harmonyos.com/npm/
    registry=https://registry.npmmirror.com/
    //artifactory.intra.ke.com/artifactory/api/npm/npm-mobile-release/:_password="bW9iaWxlZnVsbDEyMzQ1Ng=="
    //artifactory.intra.ke.com/artifactory/api/npm/npm-mobile-release/:username=mobilefull
    //artifactory.intra.ke.com/artifactory/api/npm/npm-mobile-release/:email=邮箱地址
    //artifactory.intra.ke.com/artifactory/api/npm/npm-mobile-release/:always-auth=true
    loglevel=info
    strict-ssl=false

参考链接:鸿蒙路由2.0的使用与原理

13.富文本

问题描述

[图片]
1.Text包裹Span
(1)Span不支持margin、padding、width、height,左右间距只能通过空格来处理,不精准
(2)标签不居中,需求里标签字号比第一个文本字号小,所以高度小,不居中,尝试设置lineHeight(高度不变)和baselineOffset不行(会文字挪上去了,背景不动)
(3)标签无法设置上下间距
2.RichText支持的html标签有限,实现不了

解决方案

标签使用图片,ImageSpan
[图片]

14.弹窗弹起键盘后,存在空隙

问题描述

[图片]
CustomDialog中有TextArea,输入时,软键盘顶起CustomDialog,此时,软键盘与CustomDialog之间存在一些空隙。

原因

系统特性:弹窗避让软键盘时,与软键盘之间存在16vp的安全间距。
[图片]
文档链接:自定义弹窗 (CustomDialog)
参考链接:HarmonyOS CustomDialog弹窗被软键盘顶起后,存在空隙

解决方案

.offset({ y: 16 })

15.Tab里的页面出现不走onShown

问题描述

[图片]
首页或租约页设置了keNavDestinationLifecycleCallback后,不生效。

原因

走了Tab的keNavDestinationLifecycleCallback
[图片]

解决方案

方案一:Tab传参给对应页面
方案二:页面根组件onVisibleAreaChange
.onVisibleAreaChange([0.0, 1], (isVisible: boolean, currentRatio: number) => {
if (isVisible && currentRatio >= 1.0) {
this.pageColumnFullyVisible();
}
})

16.跨页面传值

问题描述

租约页->合同详情-隐藏原因弹窗,隐藏租约后,租约页置空请求入参

解决方案

使用eventHub
租约页订阅事件:
const uiContext = appConfig.appContext.appContext;
// 订阅隐藏租约成功事件
uiContext.eventHub.on(RPLeaseEvent.HIDE_LEASE_SUCCESS, this.hideLeaseSuccessHandler.bind(this));
不用箭头函数时注意this指向
隐藏成功发送事件:
// 隐藏成功
const uiContext = appConfig.appContext.appContext;
uiContext.eventHub.emit(RPLeaseEvent.HIDE_LEASE_SUCCESS, this.order?.orderId ?? ‘’);

17.调试技巧

[图片]

18.子孙组件传值

可以使用@provider、consumer,极其优雅

19.禁用 fast-forward 合并

问题描述

开发分支有提交记录,相比master有改动,为了合并开发分支上的提交
报错:
! [remote rejected] HEAD -> refs/for/master (no new changes)
error: failed to push some refs to ‘ssh://gerrit.lianjia.com:29418/mobile_harmony/beike_rentplat_module’

解决方案

git merge –no-ff feature-branch
即使可以 fast-forward 合并,也要生成一个合并提交。

20.超出父容器

问题描述

[图片]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Column() {
// 姓名
Text(agent.name)
.fontSize($r('app.float.font_size_13'))
.fontWeight(FontWeight.Medium)
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.margin({ top: 1 })

// 描述
Text(agent.agentDesc)
.fontSize($r('app.float.font_size_11'))
.fontColor($r('app.color.999999'))
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.margin({ top: 5 })
}
.alignItems(HorizontalAlign.Start)
.justifyContent(FlexAlign.Start)
.margin({ left: 6 })
.layoutWeight(1)

确保父级 Row() 或 Column() 的子项可以收缩/限制宽度
.layoutWeight(1)
让 Column 可被压缩

更早的文章

【前端】开发Tips

1.添加 vConsole12345<script src="https://cdn.bootcss.com/vConsole/3.2.2/vconsole.min.js"></script><script> var vConsole = new VCons …

前端 阅读全文