web vue项目优化

vue click事件全局防抖

const on = Vue.prototype.$on;
Vue.prototype.$on = function(event, func) {
let timer,
    newFunc = func,
    switchon = true;
if (event === "click") {
    newFunc = function() {
        if (switchon) {
            func.apply(this, arguments);
            switchon = false;
        }
        clearTimeout(timer);
        timer = setTimeout(function() {
            switchon = true;
        }, 800);
    };
}
on.call(this, event, newFunc);
};

vue中的事件委托

 <!--   html  -->
  <ul  @click.stop ="handleClick($event)" v-if="array && array.length">
              <li :data-val='el.id v-for="el in array"'>{{el.name}}</li>
  </ul>

   // methods

  handleClick (data, e) {
    if (e.target.nodeName.toLowerCase() === 'li') {
      if (e.target.dataset && e.target.dataset.hasOwnProperty('val')) {
       this.getdatafn( e.target.dataset.val)
      }
    }
  },

视图通过route.meta..KeepAlive判断是否缓存组件

 <keep-alive v-if="$route.meta.KeepAlive">
        <router-view></router-view>
      </keep-alive>
      <router-view v-else></router-view>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。