Skip to content

Gargantua7/ohos-lifecycle

Repository files navigation

ohos-lifecycle

类 Lifecycle 接口的生命周期监听和注册以及 ViewModel 管理


如何安装

ohpm install @gargantua7/lifecycle

Startup

注册 Ability

继承LifecycleAbility

export default class EntryAbility extends LifecycleAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    super.onCreate(want, launchParam)
  }
  
  onDestroy(): void {
    super.onDestroy();
  }
}

或者在已有Ability中调用注册方法

export default class EntryAbility extends UIAbilitiy {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    LifecycleAbility.register(this)
  }
  
  onDestroy(): void {
    LifecycleAbility.unregister(this)
  }
}

方法说明

Lifecycle

在 Page / Component 中获取 Lifecycle 对象

同一个页面中无论何处获得的Lifecycle对象均为同一个

@ComponentV2
struct Component {
  private lifecycle!: Lifecycle

  aboutToAppear(): void {
    this.lifecycle = Lifecycle.getOrCreate(this)
  }
}

向 Lifecycle 中注册监听器

this.lifecycle.addObserver({
  onStateChanged: (state) => {
    promptAction.showToast({
      message: "onStateChanged " + state
    })
  }
} as LifecycleEventObserver)
this.lifecycle.addObserver({
    aboutToAppear: () => { ... },
    aboutToDisappear: () => { ... },
    onPageShow: () => { ... },
    onPageHide: () => { ... },
    onBackPress: () => { ... },
} as DefaultLifecycleObserver)

ViewModel

在 Page / Component 中获取指定 ViewModel

在同一个页面中无论何处获得相同类型同名的ViewModel实例对象均为同一个

@ComponentV2
struct Component {
	get viewModel() {
    ViewModelProvider.of(this).get(MyViewModel [, "viewModelName"])
  }
}

class MyViewModel extends ViewModel {
  
}

Closeable

将实现 Closeable的对象添加到ViewModel中,ViewModel被 clear 时将自动调用 Closeableclose 方法

class MyViewModel extends ViewModel {
  init() {
    this.addCloseable({
      close() {
        // ...
      }
    })
  }
}

Scope

提供LifecycleScopeViewModelScope

两者均仅在页面生命周期在onPageShow时才运行提交的任务,在onPageHide时暂停执行还未执行的任务,在对应的组件生命周期结束后停止执行还未执行的任务

@ComponentV2
struct Component {
  aboutToAppear(): void {
    Lifecycle.getOrCreate(this).lifecycleScope.launch(() => {
      //...
    })
  }
}
class MyViewModel extends ViewModel {
  init() {
    this.viewModelScope.launch(() => {
      //...
    })
  }
}

About

类 Lifecycle 接口的生命周期监听和注册以及 ViewModel 管理

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published