Harmony import和export

1、直接在类export 后添加了 default

定义了一个类,直接在类名前添加export default,导入使用时类名不需要放在大括号内

export default class Constants {
  static readonly MATCH_PARENT = '100%'
}

// 导入使用
import Constants from ‘./Constants’   //路径根据文件位置进行处理
TextInput({ placeholder: $r('app.string.username'), text: "11111" })
      .width(Constants.MATCH_PARENT)

2、使用export default +new组合

直接定义类,然后使用export default new +类进行到处,导入使用时类名不需要放在大括号内

class LogUtils {

  private domain: number = 0xFF00
  private format: string = '%{public}s, %{public}s'
  private tag:string="Harmony"

  debug(...args: any[]): void {
    hilog.debug(this.domain, this.tag, this.format, args)
  }

  info(...args: any[]): void {
    hilog.debug(this.domain, this.tag, this.format, args)
  }
}
export default new LogUtils()

// 导

猜你喜欢

转载自blog.csdn.net/xiaopihair123/article/details/133785946