mixin.js 6.0 KB

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