我正在尝试实现类似 Slack 的功能,在没有按下 shift 键的情况下仅在精确按下回车键时发送消息。
考虑以下 Vue 模板
<textarea type="text" v-model="message" @keyup.enter.exact="sendMessage($event)"></textarea>
以及这个组件。
export default {
name: 'Typing',
data() {
return {
message: null
}
},
methods: {
sendMessage(e) {
// e.stopPropagation() and e.preventDefault() have no impact
this.$socket.emit('message', { text: this.message });
console.log(this.message); // Print the message with another '\n' at the end due to textarea default behavior
}
}
}
有人知道如何避免在将其发送到后端之前删除最后一个 '\n',但不使用正则表达式来删除它吗?(我认为这样做会很糟糕)
谢谢
PS:我对VueJS堆栈非常陌生,希望我的问题不是显而易见的
编辑:这个问题类似,但提出的解决方案不起作用。