mescroll-mixins.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // mescroll-body 和 mescroll-uni 通用
  2. // import MescrollUni from "./mescroll-uni.vue";
  3. // import MescrollBody from "./mescroll-body.vue";
  4. const MescrollMixin = {
  5. // components: { // 非H5端无法通过mixin注册组件, 只能在main.js中注册全局组件或具体界面中注册
  6. // MescrollUni,
  7. // MescrollBody
  8. // },
  9. data() {
  10. return {
  11. list:[],
  12. mescroll: null, //mescroll实例对象
  13. downOption:{
  14. auto:false
  15. }
  16. }
  17. },
  18. // 注册系统自带的下拉刷新 (配置down.native为true时生效, 还需在pages配置enablePullDownRefresh:true;详请参考mescroll-native的案例)
  19. onPullDownRefresh(){
  20. this.mescroll && this.mescroll.onPullDownRefresh();
  21. },
  22. // 注册列表滚动事件,用于判定在顶部可下拉刷新,在指定位置可显示隐藏回到顶部按钮 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  23. onPageScroll(e) {
  24. this.mescroll && this.mescroll.onPageScroll(e);
  25. },
  26. // 注册滚动到底部的事件,用于上拉加载 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  27. onReachBottom() {
  28. this.mescroll && this.mescroll.onReachBottom();
  29. },
  30. methods: {
  31. // mescroll组件初始化的回调,可获取到mescroll对象
  32. mescrollInit(mescroll) {
  33. this.mescroll = mescroll;
  34. this.mescrollInitByRef(); // 兼容字节跳动小程序
  35. },
  36. // 以ref的方式初始化mescroll对象 (兼容字节跳动小程序)
  37. mescrollInitByRef() {
  38. if(!this.mescroll || !this.mescroll.resetUpScroll){
  39. let mescrollRef = this.$refs.mescrollRef;
  40. if(mescrollRef) this.mescroll = mescrollRef.mescroll
  41. }
  42. },
  43. reload(){
  44. this.mescroll.resetUpScroll();
  45. },
  46. // 下拉刷新的回调 (mixin默认resetUpScroll)
  47. downCallback(){
  48. setTimeout(()=>{
  49. this.mescroll.resetUpScroll();
  50. this.$u.toast('刷新成功')
  51. },1200)
  52. },
  53. // 上拉加载的回调
  54. upCallback() {
  55. // mixin默认延时500自动结束加载
  56. setTimeout(()=>{
  57. this.mescroll.endErr();
  58. }, 500)
  59. }
  60. },
  61. mounted() {
  62. this.mescrollInitByRef(); // 兼容字节跳动小程序, 避免未设置@init或@init此时未能取到ref的情况
  63. }
  64. }
  65. export default MescrollMixin;