mixin.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. export default {
  2. data() {
  3. return {}
  4. },
  5. onLoad() {
  6. // getRect挂载到$u上,因为这方法需要使用in(this),所以无法把它独立成一个单独的文件导出
  7. this.$u.getRect = this.$uGetRect
  8. },
  9. created() {
  10. // 组件当中,只有created声明周期,为了能在组件使用,故也在created中将方法挂载到$u
  11. this.$u.getRect = this.$uGetRect
  12. },
  13. computed: {
  14. // 在2.x版本中,将会把$u挂载到uni对象下,导致在模板中无法使用uni.$u.xxx形式
  15. // 所以这里通过computed计算属性将其附加到this.$u上,就可以在模板或者js中使用uni.$u.xxx
  16. // 只在nvue环境通过此方式引入完整的$u,其他平台会出现性能问题,非nvue则按需引入(主要原因是props过大)
  17. $u() {
  18. // #ifndef APP-NVUE
  19. // 在非nvue端,移除props,http,mixin等对象,避免在小程序setData时数据过大影响性能
  20. return uni.$u.deepMerge(uni.$u, {
  21. props: undefined,
  22. http: undefined,
  23. mixin: undefined
  24. })
  25. // #endif
  26. // #ifdef APP-NVUE
  27. return uni.$u
  28. // #endif
  29. },
  30. /**
  31. * 生成bem规则类名
  32. * 由于微信小程序,H5,nvue之间绑定class的差异,无法通过:class="[bem()]"的形式进行同用
  33. * 故采用如下折中做法,最后返回的是数组(一般平台)或字符串(支付宝和字节跳动平台),类似['a', 'b', 'c']或'a b c'的形式
  34. * @param {String} name 组件名称
  35. * @param {Array} fixed 一直会存在的类名
  36. * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
  37. * @returns {Array|string}
  38. */
  39. bem() {
  40. return function(name, fixed, change) {
  41. // 类名前缀
  42. const prefix = `u-${name}--`
  43. const classes = {}
  44. if (fixed) {
  45. fixed.map((item) => {
  46. // 这里的类名,会一直存在
  47. classes[prefix + this[item]] = true
  48. })
  49. }
  50. if (change) {
  51. change.map((item) => {
  52. // 这里的类名,会根据this[item]的值为true或者false,而进行添加或者移除某一个类
  53. this[item] ? (classes[prefix + item] = this[item]) : (delete classes[prefix +
  54. item])
  55. })
  56. }
  57. return Object.keys(classes)
  58. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  59. // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK
  60. .join(' ')
  61. // #endif
  62. }
  63. }
  64. },
  65. methods: {
  66. // 跳转某一个页面
  67. openPage(urlKey = 'url') {
  68. const url = this[urlKey]
  69. if (url) {
  70. // 执行类似uni.navigateTo的方法
  71. uni[this.linkType]({
  72. url
  73. })
  74. }
  75. },
  76. // 查询节点信息
  77. // 目前此方法在支付宝小程序中无法获取组件跟接点的尺寸,为支付宝的bug(2020-07-21)
  78. // 解决办法为在组件根部再套一个没有任何作用的view元素
  79. $uGetRect(selector, all) {
  80. return new Promise((resolve) => {
  81. uni.createSelectorQuery()
  82. .in(this)[all ? 'selectAll' : 'select'](selector)
  83. .boundingClientRect((rect) => {
  84. if (all && Array.isArray(rect) && rect.length) {
  85. resolve(rect)
  86. }
  87. if (!all && rect) {
  88. resolve(rect)
  89. }
  90. })
  91. .exec()
  92. })
  93. },
  94. getParentData(parentName = '') {
  95. // 避免在created中去定义parent变量
  96. if (!this.parent) this.parent = {}
  97. // 这里的本质原理是,通过获取父组件实例(也即类似u-radio的父组件u-radio-group的this)
  98. // 将父组件this中对应的参数,赋值给本组件(u-radio的this)的parentData对象中对应的属性
  99. // 之所以需要这么做,是因为所有端中,头条小程序不支持通过this.parent.xxx去监听父组件参数的变化
  100. // 此处并不会自动更新子组件的数据,而是依赖父组件u-radio-group去监听data的变化,手动调用更新子组件的方法去重新获取
  101. this.parent = this.$u.$parent.call(this, parentName)
  102. if (this.parent.children) {
  103. // 如果父组件的children不存在本组件的实例,才将本实例添加到父组件的children中
  104. this.parent.children.indexOf(this) === -1 && this.parent.children.push(this)
  105. }
  106. if (this.parent && this.parentData) {
  107. // 历遍parentData中的属性,将parent中的同名属性赋值给parentData
  108. Object.keys(this.parentData).map((key) => {
  109. this.parentData[key] = this.parent[key]
  110. })
  111. // #ifdef VUE3
  112. this.parentData.value = this.parent.modelValue;
  113. // #endif
  114. }
  115. },
  116. // 阻止事件冒泡
  117. preventEvent(e) {
  118. e && typeof(e.stopPropagation) === 'function' && e.stopPropagation()
  119. },
  120. // 空操作
  121. noop(e) {
  122. this.preventEvent(e)
  123. }
  124. },
  125. onReachBottom() {
  126. uni.$emit('uOnReachBottom')
  127. },
  128. // #ifndef VUE3
  129. beforeDestroy() {
  130. // 判断当前页面是否存在parent和chldren,一般在checkbox和checkbox-group父子联动的场景会有此情况
  131. // 组件销毁时,移除子组件在父组件children数组中的实例,释放资源,避免数据混乱
  132. if (this.parent && uni.$u.test.array(this.parent.children)) {
  133. // 组件销毁时,移除父组件中的children数组中对应的实例
  134. const childrenList = this.parent.children
  135. childrenList.map((child, index) => {
  136. // 如果相等,则移除
  137. if (child === this) {
  138. childrenList.splice(index, 1)
  139. }
  140. })
  141. }
  142. },
  143. // #endif
  144. // #ifdef VUE3
  145. beforeUnmount() {
  146. // 判断当前页面是否存在parent和chldren,一般在checkbox和checkbox-group父子联动的场景会有此情况
  147. // 组件销毁时,移除子组件在父组件children数组中的实例,释放资源,避免数据混乱
  148. if (this.parent && uni.$u.test.array(this.parent.children)) {
  149. // 组件销毁时,移除父组件中的children数组中对应的实例
  150. const childrenList = this.parent.children
  151. childrenList.map((child, index) => {
  152. // 如果相等,则移除
  153. if (child === this) {
  154. childrenList.splice(index, 1)
  155. }
  156. })
  157. }
  158. },
  159. // #endif
  160. }