gaoluyang
5 天以前 dbb01dabdc106e353aaefee80ccbdf9fb6669173
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<template>
  <div ref="content" class="scroll-pagination" @scroll="onScroll">
    <slot></slot>
    <el-button v-if="!finishLoding && isLoding" link style="display: flex; margin: 0 auto; color: var(--lims-text-3)">
      <lims-icon name="loading" style="font-size: 20px" />
    </el-button>
    <el-button v-show="finishLoding" link style="display: flex; margin: 0 auto; color: var(--lims-text-3)">
      已经没有更多啦~
    </el-button>
  </div>
</template>
 
<script>
export default {
  name: 'ScrollPagination',
  props: {
    finishLoding: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isLoding: false,
      // 原实现在 created 里把 this.onScroll 覆盖成防抖函数;Vue3 里靠给实例挂属性实现,
      // 这里改成显式状态 + 真方法,语义相同且不再是隐式覆盖。
      scrollTimer: null
    }
  },
  watch: {
    list: {
      deep: true,
      handler() {
        this.isLoding = false
      }
    }
  },
  beforeUnmount() {
    clearTimeout(this.scrollTimer)
  },
  methods: {
    onScroll() {
      clearTimeout(this.scrollTimer)
      this.scrollTimer = setTimeout(this.scrollFn, 500)
    },
    scrollFn() {
      const content = this.$refs.content
      if (content.scrollTop + content.clientHeight + 2 >= content.scrollHeight) {
        if (!this.finishLoding) {
          this.loadMore()
        } else {
          this.isLoding = false
        }
      }
    },
    loadMore() {
      if (this.isLoding) return
      this.isLoding = true
      setTimeout(() => {
        this.$emit('load')
      }, 500)
    }
  }
}
</script>
 
<style scoped>
.scroll-pagination {
  height: 100%;
  overflow-y: auto;
}
</style>