Skip to content

Can't use @Component on TypeScript abstract component class #91

Closed
@Toilal

Description

@Toilal

I wrote an abstract common class that extends Vue where I want to put some common logic, including some hook methods like created(), but those methods are not invoked by the framework.

Here's the abstract class implementing created().

abstract class DataEditorVue<T extends AbstractData> extends Vue {
  creation: boolean = false;
  data: T = null;

  created() {
    if (!this.data) {

      this.data = this.buildDataInstance();
    }
  }

  abstract buildDataInstance(): T;

  abstract getApiResource(): AbstractDataResource<T>;

  abstract getRouterPath(data: T): string;

  abstract getRouterPathAfterDeletion(data: T): string;

  remove() {
    return this.getApiResource().deleteFromObject(this.data).then(() => {
      return this.$router.push(this.getRouterPathAfterDeletion(this.data));
    });
  }

  create() {
    return this.getApiResource().create(this.data).then((data) => {
      return this.$router.push(this.getRouterPath(data), () => {
        this.creation = false;
        this.data = data;
      });
    });
  }

  update() {
    return this.getApiResource().update(this.data);
  }
}

Here the concrete class, extending abstract one.

@WithRender
@Component({
  components: {EditorControls}
})
class ObservationMilieuVue extends DataEditorVue<ObservationMilieu> {
  @Inject('observationTaxonResource')
  dataResource: ObservationTaxonResource;

  buildDataInstance(): ObservationMilieu {
    let data = new ObservationMilieu();
    data.localisation = {id: this.$route.params.localisationId};
    return data;
  }

  getApiResource(): ObservationTaxonResource {
    return this.dataResource;
  }

  getRouterPath(data: ObservationMilieu): string {
    return `/localisation/${data.localisation.id}/observation-milieu/${data.id}`;
  }

  getRouterPathAfterDeletion(data: ObservationMilieu): string {
    return `/localisation/${data.localisation.id}`;
  }
};

My actual workaround is to add a created() method in concrete class to invoke super.created() manually.

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