在uniapp框架中很少用到键盘回车键发送,但有时候也会使用。这次我的uniapp需要打包H5版本,就会用到键盘回车方便一点。
1.在对应的样式标签中写一个 键盘发送 的方法confirm-type=“send” @confirm="doSend"
//这里的e指的键盘事件传递的参数,text是绑定的输入框的内容
doSend(e, text) {
console.log(text);
if (e.ctrlKey && e.keyCode === 13) {
//用户点击了ctrl+enter触发
this.inputContext += '\n';
} else {
//用户点击了enter触发 执行一些逻辑方法
if (e !== undefined) {
//发送消息的方法
this.sendMsgs(text);
// 阻止浏览器默认的敲击回车换行的方法
e.preventDefault();
}
}
},
// 服务器发给我/我发给服务器
sendMsgs(text) {
this.inputContext = text;
const type = this.sendType;
let data = {
userId: this.clientInfoData.userId,
kefuId: this.kefuId,
target: this.target,
owner: this.clientInfoData.userId,
type
};
//根据发送数据的类型,判断
switch (type) {
case 'text':
if (!this.inputContext) {
return this.showToast('请输入内容');
}
data.data = this.inputContext;
break;
case 'image':
data.data = this.imageUrl;
// console.log(data.data);
break;
case 'music':
data.data = this.inputContext;
break;
}
// 判断对方在不在线
if (this.lineState === false) {
return this.showToast('暂无客服,请点击转人工');
} else {
//这个方法是调用vuex仓库里面的给服务器发送消息的方法
this.$store.dispatch('sendMsg', data);
}
//发送完消息之后,讲输入框内的文本清空
switch (type) {
case 'text':
this.inputContext = '';
break;
case 'image':
this.imageUrl = '';
break;
case 'music':
this.imageUrl = '';
break;
}
},