美文网首页
Vue.轮儿之popover 记录

Vue.轮儿之popover 记录

作者: clumsy钧 | 来源:发表于2018-12-07 22:32 被阅读62次

需求决定bug

xxx(){
      this.visible=!this.visible;
      if (this.visible ===true){
        document.addEventListener('click',()=>{
      this.visible=false
})
}

这里的Bug是因为当点击一次 时这两个事件都进行了
即this.visible先等于了true又等于了false 相当于什么都没干

解决方案

用异步来进行处理

  1. setTimeout
  2. vm.$nextTrick
 this.$nextTick(()=>{
        document.addEventListener('click',()=>{
      this.visible=false     };

二 事件监听完之后未取消

每次的点击都会在document上添加一个监听事件,点N次就添加了N个

解决方案

在visible变成false的时候取消监听

 this.$nextTick(()=>{
        document.addEventListener('click',function cancel(){
      this.visible=false
      document.removeEventListener('click',cancel)
     };
   };

三 用bind引来的Bug

事件监听我们必须引入bind来将真正的this传入但是
.bind会返回一个新的函数 ,如果用事件监听addEventListener的话


      document.addEventListener('click',function cancel{
          this.visible=false;
          document.removeEventListener('click')
      }).bind(this)

并不会起作用
这时候因为上面bind是返回一个新的函数,删除的事件不是一个事件的函数,因此起不到作用

解决方案

用箭头函数

if (this.visible ===true){
                    this.$nextTick(()=>{
                        let eventHandle=()=>{
                            this.visible=false;
                            document.removeEventListener('click',eventHandle)
                        };
                        document.addEventListener('click',eventHandle)
                    });
                }

还有一点是在Vue中使用添加一个事件

相关文章

  • Vue.轮儿之popover 记录

    需求决定bug 一 这里的Bug是因为当点击一次 时这两个事件都进行了即this.visible先等于了true又...

  • Bootstrap popover的使用

    Bootstrap popover的使用 html .popover { max-width: 1200p...

  • 2018-09-11

    小谷子课堂笔记之Vue. Vue.js 渐进式JavaScript,声明式渲染。 el:element 需要获取的...

  • 2018-10-13Bootstrap09

    Bootstrap 弹出框(Popover)插件 弹出框(Popover)与工具提示(Tooltip)类似,提供了...

  • 2018-09-10

    小谷子课堂笔记之Vue. 国内CDN服务网址:https://www.bootcdn.cn/ Vue.js官网:h...

  • No matching version found for fr

    react-input-color 依赖了 @xkit/popover@xkit/popover 又依赖了fron...

  • 轮滑

    众儿穿四轮鞋比跑,称之为轮滑也。站之,保平衡,脚踏地,冲也。群儿冲至于终点,以先者为胜。

  • el-popover使用

    el-popover使用

  • popOver

    //1. 创建popVC UIViewController*contentVC = [[UIViewControl...

  • Popover

    如果同一款产品同时开发iPhone版本和iPad版本,会造成开发周期过长,人力物力消耗较大的情况.为了解决这个问题...

网友评论

      本文标题:Vue.轮儿之popover 记录

      本文链接:https://www.haomeiwen.com/subject/vxgxhqtx.html