index.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. },
  50. methods: {
  51. onLoad() {
  52. this.defaultParams.pageNum++;
  53. this.list(true);
  54. if (this.data.length >= this.total) {
  55. this.finished = true;
  56. }
  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
  64. .$post(this.url, this.defaultParams)
  65. .then((res) => {
  66. if (res.success) {
  67. this.data.push(...res.data.list);
  68. this.total = res.data.total;
  69. if (bool) this.loading = false;
  70. }
  71. });
  72. },
  73. },
  74. };
  75. </script>
  76. <style lang="less" scoped></style>