原因分析:
uniapp使用vue,双向绑定组件,在input标签loop事件循环获取文本的过程中,读取文本信息不是顺序读取,如果条码中包含了回车键,比如扫描枪,扫描文本后会追加回车键,监控文本读取还没有读取完所有文本,先读取到了回车,触发了相应的回车事件导致
关键点:confirm回车事件先于 data属性值绑定,事件触发时,data值还没有完全绑定完
关键代码:在input标签中传递事件对象,通过事件多谢直接获取值,此时值是完整的
BinderConfirm(e) {
let binderCode = e.target.value
// console.log(binderCode)
this.tetsCode.binderText = this.tetsCode.binderText + "\n" + binderCode
this.blurfun();
}
unBinderConfirm(e) {
//获取文本框条码
let text = e.target.value
// this.barCode = e
// console.log(text)
this.tetsCode.unbinderText = this.tetsCode.unbinderText + "\n" + text
}
解决方案
1.不使用双向绑定
uniapp使用vue,对于扫码后,数据的重置空,不使用绑定,很难操作,第一种方案的清空重置,在html5上可以使用,在打包apk后无法使用,相对不完美
2.使用双向绑定
相对第一种更方便对数据进行清理重置,比较符合不停扫码的场景
上代码,barcodeTest.vue
<template>
<view class="uni-common-mt">
<view class="uni-padding-wrap">
<view class="cu-form-group">
<view class="title">未绑定2:</view>
<input class="input" type="text" :focus="tetsCode.isfocus2" @blur="tetsCode.isfocus2 = false" id="codeTest1" ref="inputCode" @confirm="unBinderConfirm($event)"></input>
</view>
<view class="uni-textarea" style="background-color: white;">
<textarea v-model="tetsCode.unbinderText" style="color: black;" />
</view>
<view class="tag-view panel-full margin-top">
<uni-tag text="清空条码" type="success" @click="clearBarCode()" />
</view>
<view class="tag-view panel-full margin-top">
<uni-tag text="清空" type="success" @click="tetsCode.unbinderText = '未绑定文本:'" />
</view>
<view class="cu-form-group">
<view class="title" style="color: red;">双向绑定:</view>
<input ref="testBinderCode" v-model="tetsCode.binderCode" :focus="tetsCode.isfocus" @confirm="BinderConfirm($event)" class="input" type="text"
name="codeTest2" style="color: red;" @blur="blurfun()"></input>
</view>
<view class="uni-textarea" style="background-color: white;">
<textarea v-model="tetsCode.binderText" style="color: red;" />
</view>
<view class="tag-view panel-full margin-top">
<uni-tag text="清空" type="success" @click="tetsCode.binderText = '双向绑定文本:'" />
</view>
<view class="tag-view panel-full margin-top">
<uni-tag text="清空绑定条码" type="success" @click="setValue2()" />
</view>
<view class="cu-form-group">
<view class="title" style="color: green;">主机名:</view>
<input v-model="tetsCode.host" class="input" type="text" name="codeTest2"
style="color: green;"></input>
</view>
<view class="tag-view panel-full margin-top">
<uni-tag text="获取" type="error" @click="download3()" />
</view>
</view>
</view>
</view>
</template>
<script>
import permision from "@/common/js/permission.js"
import uniTag from '@/components/uni-ui/uni-tag/uni-tag.vue';
import UniIcons from '@/components/uni-ui/uni-icon/uni-icon.vue'
import UniNumberBox from '@/components/uni-ui/uni-number-box/uni-number-box.vue'
import uniIcon from '@/components/uni-icon/uni-icon.vue'
import tTable from '@/components/t-table/t-table.vue';
import tTh from '@/components/t-table/t-th.vue';
import tTr from '@/components/t-table/t-tr.vue';
import tTd from '@/components/t-table/t-td.vue';
export default {
components: {
uniTag,
UniIcons,
UniNumberBox,
uniIcon,
tTable,
tTh,
tTr,
tTd
},
onLoad(e) {
},
data() {
return {
tetsCode: {
binderCode: "123",
binderText: "双向绑定文本:",
unbinderText: "未绑定文本:",
host: "172.30.162.129",
isfocus:true,
isfocus2:false,
barCode:"132"
}
}
},
onLoad() {
//this.circleRodObjList = []
},
methods: {
unBinderConfirm(e) {
//获取文本框条码
let text = e.target.value
// this.barCode = e
// console.log(text)
this.tetsCode.unbinderText = this.tetsCode.unbinderText + "\n" + text
},
BinderConfirm(e) {
let binderCode = e.target.value
// console.log(binderCode)
this.tetsCode.binderText = this.tetsCode.binderText + "\n" + binderCode
this.blurfun();
},
clearBarCode(){
this.$refs.inputCode.valueSync = ""
this.tetsCode.isfocus2 = true
// this.barCode.target.text = ""
// var obj = document.getElementById('codeTest1') ;
// obj.outerText = ""
// obj.focus()
// obj.outerHTML="";
// const query = uni.createSelectorQuery().in(this);
// let select = query.select('#codeTest1')
// select._component.$refs.inputCode.valueSync = ""
// query.select('#codeTest1').boundingClientRect(data => {
// console.log(data)
// }).exec();
}
,
download3() {
let downUrl = "http://" + this.tetsCode.host + ":8088/webapp/service/apk/downloadLastFile2"
plus.runtime.openURL(downUrl)
},
setValue(){
let input = this.$refs['inputCode'];
let value = input.valueSync
// let value = "aaaa"+input.toString()
// uni.showModal({
// title: "refs对象",
// duration: 1000,
// content: value,
// showCancel: false,
// success() {
// uni.hideLoading()
// }
// })
input.valueSync = ""
// input.focus = true
// var obj = document.getElementById("codeTest1");
// obj.valueSync = ""
// this.barCode = "";
// var obj = document.getElementById('codeTest1') ;
// obj.focus()
// obj.value = ""
},
// onInput(e){
// // this.barCode.target.value = e.target.value
// }
setValue2()
{
this.tetsCode.binderCode = ""
this.tetsCode.isfocus = true
},
blurfun(){
this.tetsCode.isfocus = false
}
}
}
</script>
<style>
.uni-padding-wrap {
width: 100%;
padding: 0 20upx;
}
.uni-common-mt {
width: 100%;
}
.cu-form-group {
width: 100%;
margin: 20upx 0;
}
.scroll-view_H {
width: 200;
display: flex;
flex-wrap: nowrap;
}
.scrollx_items {
text-align: center;
display: inline-block;
width: 210rpx;
box-sizing: border-box;
margin-left: 30rpx;
margin-top: 3px;
}
.t-td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.t-th {
width: 180px;
}
.tap-button-text {
color: darkcyan
}
.radio_mat {
margin-left: 40upx;
}
</style>