vuejs / vue-class-component

ES / TypeScript decorator for class-style Vue components.
MIT License
5.81k stars 429 forks source link

Lifecycle Hooks as overriden functions from Vue Instance #532

Closed arjun1194 closed 3 years ago

arjun1194 commented 3 years ago

This is a feature request. I think it would be good if a user can override lifecycle hooks instead of creating functions with the same name as lifecycle hook functions, like so

export default class MainComponent extends Vue {

      override created(){
           //overriden from vue instance
      }

      override mounted(){
             //overriden from vue instance
      }

}

Doing it like this its easier to find from IDE what lifecycle hooks we have and this is the right object oriented way as other frameworks also do it like this ( e.g. android )

arjun1194 commented 3 years ago

Implementing the interface ClassComponentHooks solved the issue. Although Generally it could have been inside Vue itself. But implementing the interface also works.

export declare interface ClassComponentHooks {
    data?(): object;
    beforeCreate?(): void;
    created?(): void;
    beforeMount?(): void;
    mounted?(): void;
    beforeUnmount?(): void;
    unmounted?(): void;
    beforeUpdate?(): void;
    updated?(): void;
    activated?(): void;
    deactivated?(): void;
    render?(): VNode | void;
    errorCaptured?(err: Error, vm: Vue, info: string): boolean | undefined;
    serverPrefetch?(): Promise<unknown>;
}

export default class MainComponent extends Vue implements ClassComponentHooks solved my issue, but i hope there was some good code documentation. Hope this helps someone else