问题描述:
在写uni-app项目的购物车时,使用NumberBox 数字输入框该插件发现点击加减输入框并没有反应。
原因分析:
后面发现插件文件的watch:方法
watch: {
value(val) {
this.inputValue = +val;
},
inputValue(newVal, oldVal) {
if (+newVal !== +oldVal) {
this.$emit(change, newVal);
this.$emit(input, newVal);
}
}
}
this.$emit(change, newVal);
this.$emit(input, newVal);
这两条代码写反了,所以导致先调用改变事件,然后才改变值,导致数字输入框改变不了值。
并且返回的值为字符串,无法改变输入框。
解决方案:
改变顺序,先改变值,再改变事件,
并且在val值前添加+号使返回的val值变成数值。
this.$emit(input, +newVal);
this.$emit(change, +newVal);