mixin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const uni_modules_uviewPlus_libs_function_index = require("../function/index.js");
  4. const uni_modules_uviewPlus_libs_function_test = require("../function/test.js");
  5. const uni_modules_uviewPlus_libs_util_route = require("../util/route.js");
  6. const mixin = {
  7. // 定义每个组件都可能需要用到的外部样式以及类名
  8. props: {
  9. // 每个组件都有的父组件传递的样式,可以为字符串或者对象形式
  10. customStyle: {
  11. type: [Object, String],
  12. default: () => ({})
  13. },
  14. customClass: {
  15. type: String,
  16. default: ""
  17. },
  18. // 跳转的页面路径
  19. url: {
  20. type: String,
  21. default: ""
  22. },
  23. // 页面跳转的类型
  24. linkType: {
  25. type: String,
  26. default: "navigateTo"
  27. }
  28. },
  29. data() {
  30. return {};
  31. },
  32. onLoad() {
  33. this.$u.getRect = this.$uGetRect;
  34. },
  35. created() {
  36. this.$u.getRect = this.$uGetRect;
  37. },
  38. computed: {
  39. // 在2.x版本中,将会把$u挂载到uni对象下,导致在模板中无法使用uni.$u.xxx形式
  40. // 所以这里通过computed计算属性将其附加到this.$u上,就可以在模板或者js中使用uni.$u.xxx
  41. // 只在nvue环境通过此方式引入完整的$u,其他平台会出现性能问题,非nvue则按需引入(主要原因是props过大)
  42. $u() {
  43. return uni_modules_uviewPlus_libs_function_index.deepMerge(common_vendor.index.$u, {
  44. props: void 0,
  45. http: void 0,
  46. mixin: void 0
  47. });
  48. },
  49. /**
  50. * 生成bem规则类名
  51. * 由于微信小程序,H5,nvue之间绑定class的差异,无法通过:class="[bem()]"的形式进行同用
  52. * 故采用如下折中做法,最后返回的是数组(一般平台)或字符串(支付宝和字节跳动平台),类似['a', 'b', 'c']或'a b c'的形式
  53. * @param {String} name 组件名称
  54. * @param {Array} fixed 一直会存在的类名
  55. * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
  56. * @returns {Array|string}
  57. */
  58. bem() {
  59. return function(name, fixed, change) {
  60. const prefix = `u-${name}--`;
  61. const classes = {};
  62. if (fixed) {
  63. fixed.map((item) => {
  64. classes[prefix + this[item]] = true;
  65. });
  66. }
  67. if (change) {
  68. change.map((item) => {
  69. this[item] ? classes[prefix + item] = this[item] : delete classes[prefix + item];
  70. });
  71. }
  72. return Object.keys(classes);
  73. };
  74. }
  75. },
  76. methods: {
  77. // 跳转某一个页面
  78. openPage(urlKey = "url") {
  79. const url = this[urlKey];
  80. if (url) {
  81. uni_modules_uviewPlus_libs_util_route.route({ type: this.linkType, url });
  82. }
  83. },
  84. // 查询节点信息
  85. // 目前此方法在支付宝小程序中无法获取组件跟接点的尺寸,为支付宝的bug(2020-07-21)
  86. // 解决办法为在组件根部再套一个没有任何作用的view元素
  87. $uGetRect(selector, all) {
  88. return new Promise((resolve) => {
  89. common_vendor.index.createSelectorQuery().in(this)[all ? "selectAll" : "select"](selector).boundingClientRect((rect) => {
  90. if (all && Array.isArray(rect) && rect.length) {
  91. resolve(rect);
  92. }
  93. if (!all && rect) {
  94. resolve(rect);
  95. }
  96. }).exec();
  97. });
  98. },
  99. getParentData(parentName = "") {
  100. if (!this.parent)
  101. this.parent = {};
  102. this.parent = uni_modules_uviewPlus_libs_function_index.$parent.call(this, parentName);
  103. if (this.parent.children) {
  104. this.parent.children.indexOf(this) === -1 && this.parent.children.push(this);
  105. }
  106. if (this.parent && this.parentData) {
  107. Object.keys(this.parentData).map((key) => {
  108. this.parentData[key] = this.parent[key];
  109. });
  110. }
  111. },
  112. // 阻止事件冒泡
  113. preventEvent(e) {
  114. e && typeof e.stopPropagation === "function" && e.stopPropagation();
  115. },
  116. // 空操作
  117. noop(e) {
  118. this.preventEvent(e);
  119. }
  120. },
  121. onReachBottom() {
  122. common_vendor.index.$emit("uOnReachBottom");
  123. },
  124. beforeUnmount() {
  125. if (this.parent && uni_modules_uviewPlus_libs_function_test.test.array(this.parent.children)) {
  126. const childrenList = this.parent.children;
  127. childrenList.map((child, index) => {
  128. if (child === this) {
  129. childrenList.splice(index, 1);
  130. }
  131. });
  132. }
  133. }
  134. };
  135. exports.mixin = mixin;