mescroll-mixins.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. upOption:{
  17. auto:true // 自动加载
  18. },
  19. }
  20. },
  21. // 注册系统自带的下拉刷新 (配置down.native为true时生效, 还需在pages配置enablePullDownRefresh:true;详请参考mescroll-native的案例)
  22. onPullDownRefresh(){
  23. this.mescroll && this.mescroll.onPullDownRefresh();
  24. },
  25. // 注册列表滚动事件,用于判定在顶部可下拉刷新,在指定位置可显示隐藏回到顶部按钮 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  26. onPageScroll(e) {
  27. this.mescroll && this.mescroll.onPageScroll(e);
  28. },
  29. // 注册滚动到底部的事件,用于上拉加载 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
  30. onReachBottom() {
  31. this.mescroll && this.mescroll.onReachBottom();
  32. },
  33. methods: {
  34. // mescroll组件初始化的回调,可获取到mescroll对象
  35. mescrollInit(mescroll) {
  36. this.mescroll = mescroll;
  37. this.mescrollInitByRef(); // 兼容字节跳动小程序
  38. },
  39. // 以ref的方式初始化mescroll对象 (兼容字节跳动小程序)
  40. mescrollInitByRef() {
  41. if(!this.mescroll || !this.mescroll.resetUpScroll){
  42. let mescrollRef = this.$refs.mescrollRef;
  43. if(mescrollRef) this.mescroll = mescrollRef.mescroll
  44. }
  45. },
  46. reload(){
  47. this.mescroll.resetUpScroll();
  48. },
  49. // 下拉刷新的回调 (mixin默认resetUpScroll)
  50. downCallback(){
  51. setTimeout(()=>{
  52. this.mescroll.resetUpScroll();
  53. this.$u.toast('刷新成功')
  54. },1200)
  55. },
  56. // 上拉加载的回调
  57. upCallback() {
  58. // mixin默认延时500自动结束加载
  59. setTimeout(()=>{
  60. this.mescroll.endErr();
  61. }, 500)
  62. }
  63. },
  64. mounted() {
  65. this.mescrollInitByRef(); // 兼容字节跳动小程序, 避免未设置@init或@init此时未能取到ref的情况
  66. }
  67. }
  68. export default MescrollMixin;