index.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <view>
  3. <view>监听到的内容:{{ inputString }}</view>
  4. <view class="">
  5. --------------------分割线-----------------------------
  6. </view>
  7. <view class="">
  8. {{text}}
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. inputString: '',
  17. inputCache: '',
  18. text:'',
  19. };
  20. },
  21. methods: {
  22. onConfirm(text) {
  23. console.log('拿到的text', text);
  24. // 此处写我们自己的逻辑
  25. this.text = text;
  26. },
  27. keypress(e) {
  28. if (e.keyCode === 66 || e.key === 'Enter') {
  29. this.inputString = this.inputCache;
  30. this.onConfirm(this.inputString);
  31. this.inputCache = '';
  32. } else {
  33. this.inputCache += e.key;
  34. }
  35. }
  36. },
  37. mounted() {
  38. // #ifdef APP-PLUS
  39. plus.key.addEventListener("keyup", this.keypress);
  40. // #endif
  41. // #ifdef H5
  42. document.addEventListener("keyup", this.keypress);
  43. // #endif
  44. },
  45. beforeDestroy() {
  46. // #ifdef APP-PLUS
  47. plus.key.removeEventListener("keyup", this.keypress);
  48. // #endif
  49. // #ifdef H5
  50. document.removeEventListener("keyup", this.keypress);
  51. // #endif
  52. },
  53. deactivated() {
  54. // #ifdef APP-PLUS
  55. plus.key.removeEventListener("keyup", this.keypress);
  56. // #endif
  57. // #ifdef H5
  58. document.removeEventListener("keyup", this.keypress);
  59. // #endif
  60. }
  61. };
  62. </script>