I am expecting from below code that when we input minus sign, vue
will replace nevative value to positive.
<input
type="number"
:value="localValue"
@input="$event => { onInput($event.target.value); }"
/>
import { Vue, Component, Prop, Emit, Watch, Model } from "vue-property-decorator";
@Component
export default class InputField extends Vue implements IValidatableControl {
@Model("input", { required: true, type: String }) private readonly value!: string;
@Prop({ default: false, type: Boolean }) private readonly forbidMinusSign!: boolean;
private localValue: string = "";
private created(): void {
this.localValue = this.value;
}
private onInput(newActualValue: string): void {
console.log("inputted value:");
console.log(newActualValue);
if (this.forbidMinusSign && newActualValue.includes("-")) {
this.localValue = newActualValue.replace("-", "");
console.log("localValue:");
console.log(this.localValue);
return;
}
this.localValue = newActualValue;
this.$emit("input", newActualValue);
}
}
I get correct debug output:
"inputted value:"
-1234567890123
"localValue:"
1234567890123
But in input field -1234567890123
does not change to 1234567890123
. Why?
Why I don't use v-model?
Maybe I should. But first I want to understand why it does not work without v-model. I am expecting that binded input field value will change according respective variable - if I don't misunderstand the documentation, it's a default behaviour.
Also, just v-bind required for input filtering for modification. v-model
has some modifiers, but it could not be conditional.