mixin.js 4.1 KB

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