mescroll-more-item.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * mescroll-more-item的mixins, 仅在多个 mescroll-body 写在子组件时使用 (参考 mescroll-more 案例)
  3. */
  4. const MescrollMoreItemMixin = {
  5. // 支付宝小程序不支持props的mixin,需写在具体的页面中
  6. // #ifndef MP-ALIPAY || MP-DINGTALK
  7. props:{
  8. i: Number, // 每个tab页的专属下标
  9. index: { // 当前tab的下标
  10. type: Number,
  11. default(){
  12. return 0
  13. }
  14. }
  15. },
  16. // #endif
  17. data() {
  18. return {
  19. downOption:{
  20. auto:false // 不自动加载
  21. },
  22. upOption:{
  23. auto:false // 不自动加载
  24. },
  25. isInit: false // 当前tab是否已初始化
  26. }
  27. },
  28. watch:{
  29. // 监听下标的变化
  30. index(val){
  31. if (this.i === val && !this.isInit) this.mescrollTrigger()
  32. }
  33. },
  34. methods: {
  35. // 以ref的方式初始化mescroll对象 (兼容字节跳动小程序)
  36. mescrollInitByRef() {
  37. if(!this.mescroll || !this.mescroll.resetUpScroll){
  38. // 字节跳动小程序编辑器不支持一个页面存在相同的ref, 多mescroll的ref需动态生成, 格式为'mescrollRef下标'
  39. let mescrollRef = this.$refs.mescrollRef || this.$refs['mescrollRef'+this.i];
  40. if(mescrollRef) this.mescroll = mescrollRef.mescroll
  41. }
  42. },
  43. // mescroll组件初始化的回调,可获取到mescroll对象 (覆盖mescroll-mixins.js的mescrollInit, 为了标记isInit)
  44. mescrollInit(mescroll) {
  45. this.mescroll = mescroll;
  46. this.mescrollInitByRef && this.mescrollInitByRef(); // 兼容字节跳动小程序
  47. // 自动加载当前tab的数据
  48. if(this.i === this.index){
  49. this.mescrollTrigger()
  50. }
  51. },
  52. // 主动触发加载
  53. mescrollTrigger(){
  54. this.isInit = true; // 标记为true
  55. if (this.mescroll) {
  56. if (this.mescroll.optDown.use) {
  57. this.mescroll.triggerDownScroll();
  58. } else{
  59. this.mescroll.triggerUpScroll();
  60. }
  61. }
  62. }
  63. }
  64. }
  65. export default MescrollMoreItemMixin;