美文网首页
鸿蒙开发问题记录

鸿蒙开发问题记录

作者: 旺仔_100 | 来源:发表于2023-10-09 19:23 被阅读0次

目前已经在学习鸿蒙了,会持续记录一些问题。

  1. 鸿蒙中的写new 和不写new有什么分别吗?clip中不写new就会直接崩溃。
   Path()
          .commands(this.rightPath)
          .fill('none')
          .linearGradient({angle:30,colors:[['#C4FFA0', 0],['#ffffff', 1]]})
          .clip(new Path().commands(this.rightPath))
  • 使用 new 创建对象实例:

当您使用 new 关键字创建对象时,它将调用对象的构造函数(如果有的话)来初始化对象,并返回对象的实例。
这种方法通常用于创建具有特定属性和方法的自定义对象,或者创建标准库提供的对象,例如 new Date() 用于创建日期对象。

  • 不使用 new 创建对象字面量:

当您不使用 new 关键字创建对象时,它将创建一个对象字面量,这是一种轻量级的方式来创建和初始化对象,通常用于创建简单的数据结构。
这种方法通常用于创建临时对象,或者初始化对象字面量,而不涉及自定义构造函数。

2.下面代码中@Builder和@Component看着都是抽取组建。但是他们是不同的意思。@Builder似乎是用在@Component内部进行构造界面的。

@Entry
@Component
struct Home {
  @State currentTabIndex: number = 0
  @StorageProp('currentBreakpoint') currentBreakpoint: string = 'sm'
  private breakpointSystem: BreakpointSystem = new BreakpointSystem()

  @Builder
  bottomBarItemBuilder(name: Resource, icon: Resource, index: number) {
    Flex({
      direction: new BreakPointType({
        sm: FlexDirection.Column,
        md: FlexDirection.Row,
        lg: FlexDirection.Column
      }).getValue(this.currentBreakpoint),
      justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Center
    }) {
      Image(icon)
        .height(24)
        .width(24)
        .fillColor(this.getTabBarColor(index))
      Text(name)
        .margin(new BreakPointType<Padding>({
          sm: { top: 4 },
          md: { left: 8 },
          lg: { top: 4 }
        }).getValue(this.currentBreakpoint) as Padding)
        .fontSize(11)
        .fontColor(this.getTabBarColor(index))
    }
  }

  aboutToAppear() {
    this.breakpointSystem.register()
  }

  aboutToDisappear() {
    this.breakpointSystem.unregister()
  }

  build() {
    Tabs({
      barPosition: new BreakPointType({
        sm: BarPosition.End,
        md: BarPosition.End,
        lg: BarPosition.Start
      }).getValue(this.currentBreakpoint)
    }) {
      TabContent() {
        FoodsDisplay()
      }.tabBar(this.bottomBarItemBuilder($r("app.string.tab_bar_home"), $r("app.media.ic_bottom_home"), 0))

      TabContent() {
        Records()
      }.tabBar(this.bottomBarItemBuilder($r("app.string.tab_bar_record"), $r("app.media.ic_bottom_record"), 1))
    }
    .vertical(new BreakPointType({ sm: false, md: false, lg: true }).getValue(this.currentBreakpoint) as boolean)
    .barWidth(new BreakPointType({ sm: '100%', md: '100%', lg: '56vp' }).getValue(this.currentBreakpoint) as string)
    .barHeight(new BreakPointType({ sm: '56vp', md: '56vp', lg: '60%' }).getValue(this.currentBreakpoint) as string)
    .animationDuration(0)
    .onChange((index) => {
      this.currentTabIndex = index
    })
  }

  private getTabBarColor(index: number) {
    return this.currentTabIndex == index ? $r('app.color.tab_bar_select_color') : $r('app.color.tab_bar_normal_color')
  }
}

相关文章

网友评论

      本文标题:鸿蒙开发问题记录

      本文链接:https://www.haomeiwen.com/subject/uunibdtx.html