ハーモニー輸出入

1. クラスのエクスポートの直後にデフォルトを追加します

クラスを定義します。クラス名の直前に追加します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. エクスポートのデフォルトと新しい組み合わせを使用します

クラスを直接定義し、どこでもエクスポート デフォルトの新規 + クラスを使用します。インポート時にクラス名を中括弧で囲む必要はありません。

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