lodash.throttle实现节流

  1. 安装
    yarn install lodash.throttle --save

  2. 使用

<template>
  <div>
    test
  </div>
</template>

<script>
import throttle from 'lodash.throttle';

export default {
  name: 'Test',
  mounted() {
    window.addEventListener(
      'resize',
      (this.resize = throttle(this.resizeFun, 100)),
    );
  },
  destroyed() {
    if( this.resize ) 
        window.removeEventListener('resize', this.resize);
  },
  methods: {
    resizeFun() {
      console.log('视口改变');
    },
  },
};
</script>
posted @ 2019-12-26 10:35  ฅ˙-˙ฅ  阅读(420)  评论(0编辑  收藏  举报