clipboard.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-title">请输入剪贴板内容</view>
  6. <view class="uni-list">
  7. <view class="uni-list-cell">
  8. <input class="uni-input" type="text" placeholder="请输入剪贴板内容" :value="data" @input="dataChange"/>
  9. </view>
  10. </view>
  11. <view class="uni-btn-v">
  12. <button type="primary" @click="setClipboard">存储数据</button>
  13. <button @tap="getClipboard">读取数据</button>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. title: 'get/setClipboardData',
  23. data: ''
  24. }
  25. },
  26. methods: {
  27. dataChange: function (e) {
  28. this.data = e.detail.value
  29. },
  30. getClipboard: function () {
  31. uni.getClipboardData({
  32. success: (res) => {
  33. console.log(res.data);
  34. const content = res.data ? '剪贴板内容为:' + res.data : '剪贴板暂无内容';
  35. uni.showModal({
  36. content,
  37. title: '读取剪贴板',
  38. showCancel: false
  39. })
  40. },
  41. fail: () => {
  42. uni.showModal({
  43. content: '读取剪贴板失败!',
  44. showCancel: false
  45. })
  46. }
  47. });
  48. },
  49. setClipboard: function () {
  50. var data = this.data;
  51. if (data.length === 0) {
  52. uni.showModal({
  53. title: '设置剪贴板失败',
  54. content: '内容不能为空',
  55. showCancel: false
  56. })
  57. } else {
  58. uni.setClipboardData({
  59. data: data,
  60. success: () => {
  61. // 成功处理
  62. // #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  63. uni.showToast({
  64. title: '设置剪贴板成功',
  65. icon: "success",
  66. mask: !1
  67. })
  68. // #endif
  69. },
  70. fail: () => {
  71. // 失败处理
  72. // #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO
  73. uni.showToast({
  74. title: '储存数据失败!',
  75. icon: "none",
  76. mask: !1
  77. })
  78. // #endif
  79. }
  80. });
  81. }
  82. }
  83. }
  84. }
  85. </script>
  86. <style>
  87. </style>