index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <div>
  3. <van-list
  4. class="load-list"
  5. v-model="loading"
  6. @load="onLoad"
  7. :finished="finished"
  8. offset="10"
  9. :immediate-check="false"
  10. v-if="data.length"
  11. >
  12. <slot :list="data"></slot>
  13. </van-list>
  14. <van-empty :description="description" v-else />
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'loadList',
  20. props: {
  21. url: {
  22. type: String,
  23. default: '',
  24. },
  25. description: {
  26. type: String,
  27. default: '',
  28. },
  29. params: {
  30. type: Object,
  31. default: () => {
  32. return {};
  33. },
  34. },
  35. },
  36. data() {
  37. return {
  38. loading: false,
  39. finished: false,
  40. defaultParams: {
  41. pageNum: 1,
  42. pageSize: 50,
  43. },
  44. data: [],
  45. total: 0,
  46. };
  47. },
  48. mounted() {},
  49. methods: {
  50. onLoad() {
  51. if (this.data.length >= this.total) {
  52. this.finished = true;
  53. return;
  54. }
  55. this.defaultParams.pageNum++;
  56. this.list(true);
  57. },
  58. list(bool = false) {
  59. if (Object.keys(this.params).length) {
  60. this.data = [];
  61. Object.assign(this.defaultParams, this.params);
  62. }
  63. this.$axios.$post(this.url, this.defaultParams).then((res) => {
  64. if (res.success) {
  65. this.data.push(...res.data.list);
  66. this.total = res.data.total;
  67. if (bool) this.loading = false;
  68. }
  69. });
  70. },
  71. },
  72. };
  73. </script>
  74. <style lang="less" scoped></style>