I'm using Vue v2 with Typescript and I'm trying to extend a parent class:
My parent class is called BaseSelect and looks like this:
<template>
<select :value="value" @change="$emit('change', $event.target.value)">
<option value="">default option</option>
<slot />
</select>
</template>
<script lang="ts">
import { Component, Model, Vue } from 'vue-property-decorator';
@Component({})
export default class BaseSelect extends Vue {
@Model('change', { type: String, required: false, default: '' })
private readonly value!: string
private valid = true;
validate(): boolean {
this.valid = !!this.value;
return this.valid;
}
}
</script>
My child class BaseSelectGender looks like this:
<template>
<base-select :value="value" @change="$emit('change', $event)">
<option value="male">I'm male</option>
<option value="female">I'm female</option>
</base-select>
</template>
<script lang="ts">
import { Component } from 'vue-property-decorator';
import { BaseSelect } from '@/components/base';
@Component({
components: { BaseSelect }
})
export default class BaseSelectGender extends BaseSelect {}
</script>
When I use <base-select-gender> in my code there are two instances of the BaseSelect component (and therefore two different instances of the valid variable):
- The first instance which is created because of the inheritance
- The second instance which is created because of the usage of
<base-select>in the child
This leads to some problems when the valid variable changes because the wrong instance of the variable is reflected in the DOM.
So my question is now: How can I extend a base class and also use it or at least extend the html code in the template part of my child component?