Skip to content

[discussion] Add LifecycleHook interface definition #338

Closed
@CarterLi

Description

@CarterLi
// lifecycle.d.ts
import Vue from 'vue';

interface LifecycleHook {
  /** Called synchronously immediately after the instance has been initialized, before data observation and event/watcher setup. */
  beforeCreate?(): void;
  /** Called synchronously after the instance is created. */
  created?(): void;
  /** Called right before the mounting begins: the render function is about to be called for the first time. */
  beforeMount?(): void;
  /** Called after the instance has been mounted, where el is replaced by the newly created vm.$el. */
  mounted?(): void;
  /** Called when data changes, before the DOM is patched. */
  beforeUpdate?(): void;
  /** Called after a data change causes the virtual DOM to be re-rendered and patched. */
  updated?(): void;
  /** Called when a kept-alive component is activated. */
  activated?(): void;
  /** Called when a kept-alive component is deactivated. */
  deactivated?(): void;
  /** Called right before a Vue instance is destroyed. */
  beforeDestroy?(): void;
  /** Called after a Vue instance has been destroyed. */
  destroyed?(): void;
  /** Called when an error from any descendent component is captured. */
  errorCaptured?(err: Error, vm: Vue, info: string): boolean | void;
}

// Usage
export default class YourComponent extends Vue implements LifecycleHook {
  mounted() {
  }
}

Inspired by Angular lifecycle hook interfaces.

Instead of define every hook method in separated interfaces, I prefer defining them in one interface in order not to introduce redundant implement OnXxxs. But at the same time as all of those methods are optional, it generally disables the strict type checking, which makes it mainly for IDE's intellisense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions