JavaScript/vue.js/事件车

来自康健生活
Kangkk​(留言 | 贡献)2020年9月11日 (五) 03:29的版本 (→引言)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

引言

在传值中一种通用的方法是使用事件车,我一般用它做分页组件的传值处理(分页组件现在改用 store 处理)

格式

事件车的格式

在子组件,如分页组件中使用:

<!-- @auther kangkk
  @date 2020/7/13
  @desc 分页组件 -->
<template>
  <el-pagination v-if="isShowPagination"
                 background
                 :class="$style.pagination"
                 :style="{
                   width: width,
                   height: heightVaule,
                   padding: paddingValue,
                 }"
                 :current-page.sync="currentPage"
                 :page-size="pageSize"
                 :pager-count="pageCount"
                 layout="pager, jumper"
                 :total="total"
                 @size-change="handleSizeChange"
                 @current-change="handleCurrentChange"/>
</template>
<style module lang="scss">@import "./index";</style>
<script>
  import EventBus from '@@/plugins/bus.js'
  export default {
    name: 'ListPagination',
    data () {
      return {
        height: 30,
        currentPage: 1,
        pageCount: 5,
        total: 0,
        pageSize: 10,
      }
    },
    props:{
      width:{
        type: String,
        default:()=>'700px'
      },
      padding:{
        type: Array,
        default:()=>[0,0,0,0]
      }
    },
    computed: {
      isShowPagination () {
        return this.total > this.pageSize
      },
      heightVaule (){
        return this.padding[0] + this.height + this.padding[2] + 'px'
      },
      paddingValue (){
        return this.padding.join('px ')
      }
    },
    created () {
      this.servicePaginationEventMessage()
    },
    methods: {
      handleSizeChange (pageSize) {
        this.paginationEvent(pageSize, this.currentPage)
      },
      handleCurrentChange (currentPage) {
        this.paginationEvent(this.pageSize, currentPage)
      },
      /**
       * 事件车发送客户端分页数据的第几页
       */
      paginationEvent (pageSize, currentPage) {
        EventBus.$emit('clientPagination', {
          total: this.total,
          currentPage: currentPage,
          pageCount: this.pageCount,
          pageSize: pageSize
        })
      },
      /**
       * 事件车接收服务端分页数据的第几页
       */
      servicePaginationEventMessage () {
        const _self = this
        EventBus.$on('servicePagination', (event) => {
          console.log(event)
          _self.currentPage = event.currentPage
          _self.total = event.total
          _self.pageSize = event.pageSize || 10
        })
      }
    }
  }
</script>

在父组件中调用分页

<template>
  <div class="lattice-width padding-top">
    <ly-pagination class="padding-top text-center"
                   :padding="[20,0,20,0]"
                   :width="'1010px'"/>
  </div>
</template>

<script>
  import ComPagination from '@@/components/pagination/index.vue';
  import EventBus from '@@/plugins/bus.js';

  export default {
    name: 'Home',
    scrollToTop: true,
    components: {
      'ly-pagination': ComPagination
    },
    mounted () {
      this.servicePaginationEvent();
    },
    methods: {
      /**
       * 事件车发送服务端传来的分页信息
       * @param allNum
       * @param paginationCurrentPage
       * @param searchTab
       */
      servicePaginationEvent (allNum, paginationCurrentPage, searchTab) {
        EventBus.$emit('servicePagination', {
          total: 100,
          currentPage: 1
        });
      },
    }
  };
</script>