In thread Which limitations v-model has in Vue 2.x?, I learned how to link parent and child components v-model
. The suggested solution is:
--- ParentTemplate:
<Child v-model="formData"></Child>
-- ChildTemplate:
<input v-model="localValue">
-- ChildScript:
computed: {
localValue: {
get() {
return this.value;
},
set(localValue) {
this.$emit('input', localValue);
},
},
},
Unfortunately, I could not re-write it to vue-class-component syntax. Below code neither works nor should work:
export default class TextEditor extends Vue {
@Prop({ type: String, required: true }) private readonly value!: string;
private get localValue(): string {
return this.value;
}
private set localValue(newValue: string) {
this.$emit("input", newValue);
}
}
The answer to question how to write computed setters in class-based components in vuejs is not applicable to vue component properties because properties are readonly. So I can't write this.value = newValue
.
Problem with straight value
usage##
<EditorImplementation
:value="value"
@input="(value) => { onInput(value) }"
/>
@Component({
components {
EditorImplementation: CK_Editor.component
}
})
export default class TextEditor extends Vue {
@Prop({ type: String, required: true }) private readonly value!: string;
@Emit("input")
private onInput(value: string): void {
console.log("checkpoint");
console.log(this.value);
}
}
Assume that initially value is empty string.
- Input "f"
- Log will be
"checkpoint" ""
- Input "a"
- Log will be
"checkpoint" "f"
- Input "d"
- Log will be
"checkpoint" "fa"
And so on.
value
props directly in your template. Something like<input :value="value">
– TonyEditorImplementation(:value="value" @input="(value) => { onInput(value) }")
, in@Emit("input") private onInput(value: string): void { console.log(this.value); }
I always get output with previousvalue
, not actual. E. g. on first input,value
is still empty string. Is it normal? – Takeshi Tokugawa YD