Skip to content

生命周期

所有 Bean 都可以提供两个生命周期方法

名称说明
__init__在创建bean实例时执行的初始化方法,并且支持异步
__dispose__在bean实例销毁时执行的销毁方法
  • Zova 提供了两个代码片段,你可以通过输入initdispose快速添加相应的生命周期方法

举例:$useComputed

typescript
export class Counter {
  count: number = 0;
  count2: string;

  protected async __init__() {
    this.count2 = this.$useComputed(() => {
      return `=== ${this.count} ===`;
    });
  }
}

举例:$watch

typescript
export class Counter {
  count: number = 0;

  protected async __init__() {
    this.$watch(
      () => this.count,
      () => {
        console.log('changed: ', this.count);
      },
    );
  }
}

基于 MIT 许可发布