I have a child component which is an HTML input element. It looks like this:
<label-input inputId="formProductId"
inputType="number"
inputName="productId"
:inputEnabled="filters.productId.options"
:label="translations['product_id']"
:placeholder="translations['product_id']"
:data="filters.productId.value"
/>
LabelInput.vue file:
<template>
<div class="form-element">
<label :for="inputId" :class="['form-element-title', { 'js-focused': focused }]">{{ label }}</label>
<input @input="updateValue($event.target.value)" :type="inputType" :id="inputId" :name="inputEnabled ? inputName : false" v-model="inputData" :placeholder="placeholder" @focus="focused = true" @blur="focused = false">
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component
export default class LabelInput extends Vue {
@Prop({ default: () => '' }) inputId!: string
@Prop({ default: () => '' }) inputType!: string
@Prop({ default: () => '' }) inputName!: string
@Prop({ default: () => '' }) label!: string
@Prop({ default: () => '' }) placeholder!: string
@Prop({ default: () => {} }) data!: []
@Prop({ default: () => true }) inputEnabled!: string
private inputData = this.data
private focused = false
updateValue (value: string) {
this.$root.$emit('input', {'inputName' : this.inputName, 'inputValue' : value});
}
}
So the model is passed via filters.productId.value as data and then changed into local variable inputData.
Now if I change the input value I use updateValue function which emits the input name and it's value back into parent component, which updates the original filters.productId.value value. This works fine.
Now the problem is that I have a function on my parent component which resets the filters.productId.value value back to null. It looks like this:
clearFilters() {
for (const [key, value] of Object.entries(this.filters)) {
this.filters[key].value = null;
}
}
And now the data in my child component LabelInput doesn't change, even though filters.productId.value is null now. How do I refresh the child component?
this.datadidn't sync tothis.inputDatawhen parent component changedthe props=data. so one solution is add one watch forthis.data, when its value is changed, letthis.inputData=this.datain yourLabelInput.vue