【每日学点HarmonyOS Next知识】延时任务打开窗口、线性渐变、路由传参、固定浅色模式、传参问题

47 阅读2分钟

1、HarmonyOS setTimeout延时超过5秒时,无法使用window.open打开新窗口?

试验在window上也是这样, 前端js在 setTimeout 延时超过5秒时,浏览器会认为这是一个长时间的操作,可能会阻止一些操作,包括使用 window.open 打开新窗口。这是因为浏览器为了安全起见,防止攻击者利用长时间的 setTimeout 来执行广告或恶意代码,可以尝试其他方法打开窗口

2、HarmonyOS linearGradient属性在Text组件下面不生效?

.linearGradient({
  direction: GradientDirection.Right, // 渐变方向
  repeating: false, // 渐变颜色是否重复
  colors: [['#F7F8FA', 0.0], ['#F7F8FA', 1.0]]
})

这个写到Text组件下面不生效。 参考demo:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Row() {
          Text(this.message)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .blendMode(BlendMode.DST_IN, BlendApplyType.OFFSCREEN)
        }.linearGradient({
          direction: GradientDirection.Right,
          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
        }).blendMode(BlendMode.SRC_OVER, BlendApplyType.OFFSCREEN)
      }
      .width('100%')
    }
    .height('100%')
  }
}

3、HarmonyOS 在使用router.pushUrl({url: url, params: params})进行页面间传递数据时无法接收map类型参数?

router不支持map类型的数据传参,params中只能包含基础类型的数据.可以使用Record代替map

private routerParams : RouterParams = new RouterParams()
private param : Record<string, string> = {};
routePage() {
  try {
    this.param['vip_source'] = 'qww'
    this.routerParams.pageType = '11'
    this.routerParams.params = this.param
    this.routerParams.title = '新闻'
    router.pushUrl({
      url: 'pages/RouterA',
      params: this.routerParams
    })
  } catch (err) {
    console.info(` fail callback, code: ${err.code}, msg: ${err.msg}`)
  }
}

@State value:Record<string, Record<string,string>> = router.getParams() as Record<string, Record<string,string>>;

onPageShow() {
  console.log("hh2:" + this.value['title'])
  console.log("hh3:" + this.value['params']['vip_source'])
}

4、HarmonyOS 深色模式暂未适配,如何配置工程都固定在浅色模式显示?

深色模式暂未适配,如何配置工程都固定在浅色模式显示

应用默认配置为跟随系统切换深浅色模式,如不希望应用跟随系统深浅色模式变化,可主动设置应用的深浅色风格。设置后,应用的深浅色模式固定,不会随系统改变。

5、HarmonyOS 参数传参问题?

第一个问题:this.highSheepNum = this.data.totalCount()为什么赋不上值;第二个问题:点击筛选后,this.data.totalCount()值应该已经变了,怎么动态改变在Tabs TabContent中 this.highSheepNum的值,实现页面数据刷新。

第一个问题是因为changeData函数和init函数里的for循环条件里的第二个条件不可等于this.listDate.length,否则会取到this.listDate[30],超出数组长度导致报错,改为小于即可

for (let i = 0; i < this.listDate.length; i++) {

第二个问题参考:

Tabs({ barPosition: BarPosition.Start }) {
  TabContent() {
    this.highSpeedBuilder()
  }.tabBar(this.TabRoadBuilder(0, '优惠券')) // 去掉第三个参数
  TabContent() {
    this.tollStationsBuilder()
  }.tabBar(this.TabRoadBuilder(1, '收费站')) // 去掉第三个参数
}

同理,TabRoadBuilder函数去掉第三个参数,用变量this.highSheepNum作为展示,即可实现页面数据刷新

@Builder TabRoadBuilder(index: number, name: string,) { // 删除原第三个参数
  Column() {
    Text(name+'('+`${this.highSheepNum}`+')') // 使用this.highSheepNum
      .fontColor(this.currentIndex === index ? '#015DFF' : '#999999')
      .fontSize(16)
      .fontWeight(this.currentIndex === index ? 500 : 400)
      .lineHeight(22)
      .margin({ top:6, bottom: 6 })
    Divider().strokeWidth(3).color(this.currentIndex === index ? '#015DFF' : '#999999').width(24)
  }.width('100%')
}

##鸿蒙核心技术##鸿蒙开发工具##DevEco Studio## ##社交##