myInput.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // myComponents/myInput/myInput.js
  2. Component({
  3. options: {
  4. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  5. },
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. value:{
  11. type:String,
  12. value:''
  13. },
  14. placeholder:{
  15. type:String,
  16. value:'请输入'
  17. },
  18. showEdit:{
  19. type:Boolean,
  20. value:true
  21. }
  22. },
  23. // 监听传入的变量,当传入的值发生变化时,触发方法
  24. observers: {
  25. 'value': function (val) {
  26. // console.log(val);
  27. }
  28. },
  29. /**
  30. * 页面的初始数据
  31. */
  32. data: {
  33. content:'',
  34. show:false,
  35. fileList:[]
  36. },
  37. methods:{
  38. changeInput(e){
  39. const value = e.detail.value;
  40. this.triggerEvent("input", value);
  41. },
  42. inputBlur(e){
  43. const value = e.detail.value;
  44. if(value == this.data.content){
  45. return false
  46. }
  47. this.changeContent(value)
  48. this.triggerEvent("change", value);
  49. },
  50. changeContent(value){
  51. this.setData(
  52. {
  53. content:value
  54. }
  55. )
  56. },
  57. clickInnerIcon(e){
  58. if(this.properties.showEdit){
  59. // this.data.show = !this.data.show
  60. this.setData(
  61. {
  62. show:!this.data.show
  63. }
  64. )
  65. }
  66. console.log(this.properties.showEdit,this.data.show)
  67. this.triggerEvent('clickInnerIcon',1)
  68. },
  69. buttonClick(e){
  70. // console.log(e,this.properties.value)
  71. this.triggerEvent('search',1)
  72. },
  73. //图片上传
  74. afterRead(e){
  75. console.log(e)
  76. },
  77. onChange(e) {
  78. console.log('1', e)
  79. const { file, fileList } = e.detail
  80. if (file.status === 'uploading') {
  81. this.setData({
  82. progress: 0,
  83. })
  84. wx.showLoading()
  85. } else if (file.status === 'done') {
  86. this.setData({
  87. imageUrl: file.url,
  88. })
  89. }
  90. // Controlled state should set fileList
  91. this.setData({ fileList })
  92. },
  93. },
  94. /**
  95. * 组件的生命周期函数列表
  96. */
  97. lifetimes: {
  98. // 在组件实例进入页面节点树时执行
  99. attached: function () {
  100. // 初始化操作
  101. // console.log('组件初始化');
  102. this.changeContent(this.properties.value)
  103. // ...
  104. },
  105. // 在组件实例被移除出页面节点树时执行
  106. detached: function () {
  107. // 清理工作
  108. console.log('组件销毁');
  109. // ...
  110. },
  111. // ...
  112. },
  113. /**
  114. * 组件所在页面的生命周期函数可以在 pageLifetimes 中定义
  115. *
  116. * 页面生命周期函数:
  117. * show:页面显示
  118. * hide:页面隐藏
  119. * resize:页面尺寸变化
  120. */
  121. pageLifetimes: {
  122. // ...
  123. }
  124. })