useMescrollComp.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ref } from 'vue';
  2. // 小程序无法在hook中使用页面级别生命周期,需单独传入: https://ask.dcloud.net.cn/question/161173
  3. // import { onPageScroll, onReachBottom, onPullDownRefresh} from '@dcloudio/uni-app';
  4. /**
  5. * mescroll-body写在子组件时,需通过useMescrollComp补充子组件缺少的生命周期, 相当于vue2的mescroll-comp.js文件
  6. * 必须传入onPageScroll, onReachBottom
  7. * 当down.native为true时,需传入onPullDownRefresh
  8. */
  9. function useMescrollComp(onPageScroll, onReachBottom, onPullDownRefresh){
  10. // 因为子组件无onPageScroll和onReachBottom的页面生命周期,需在页面传递进到子组件
  11. onPageScroll(e=>{
  12. handlePageScroll(e)
  13. })
  14. onReachBottom(()=>{
  15. handleReachBottom()
  16. })
  17. // 当down的native: true时, 还需传递此方法进到子组件
  18. onPullDownRefresh && onPullDownRefresh(()=>{
  19. handlePullDownRefresh()
  20. })
  21. const mescrollItem = ref(null)
  22. const handlePageScroll = (e)=>{
  23. const mescroll = getMescroll()
  24. mescroll && mescroll.onPageScroll(e);
  25. }
  26. const handleReachBottom = ()=>{
  27. const mescroll = getMescroll()
  28. mescroll && mescroll.onReachBottom();
  29. }
  30. const handlePullDownRefresh = ()=>{
  31. const mescroll = getMescroll()
  32. mescroll && mescroll.onPullDownRefresh();
  33. }
  34. const getMescroll = ()=>{
  35. if(mescrollItem.value && mescrollItem.value.getMescroll){
  36. return mescrollItem.value.getMescroll()
  37. }
  38. return null
  39. }
  40. return {
  41. mescrollItem,
  42. getMescroll
  43. }
  44. }
  45. export default useMescrollComp