Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue版本适配bug #77

Closed
keenjaan opened this issue Dec 2, 2017 · 0 comments
Closed

vue版本适配bug #77

keenjaan opened this issue Dec 2, 2017 · 0 comments

Comments

@keenjaan
Copy link

keenjaan commented Dec 2, 2017

当使用vue-router切换路由时,上一个页面销毁时,所有绑定事件的元素都会触发doUnbindEvent函数,当一个元素绑定多个事件时,doUnbindEvent函数会触发多次。对于一个元素如下:

    <div v-finger:tap="tapFunc" v-finger:long-tap="longTapFunc">按钮</div>

doUnbindEvent函数:

    var doUnbindEvent = function(elem) {
      var index = getElemCacheIndex(elem);
    
      if ( index ) {
        return true;
      }
      if(!isNaN(index)) {
        var delArr = CACHE.splice(index, 1);
        if(delArr.length && delArr[0] && delArr[0].alloyFinger.destroy) {
          delArr[0].alloyFinger.destroy();
        }
      }
    };

第一次触发doUnbindEvent函数, index一定能返回一个number类型数字,会从CACHE中删除该元素。

当第二次触发doUnbindEvent时,由于该元素已被删除,所以index会返回null,而if条件并不能拦截null这个值,

    if(!isNaN(index)) {
      //
    }
    故:
    delArr = CACHE.splice(index, 1) = CACHE.splice(null, 1) = CACHE.splice(0, 1);

变成了始终截取CACHE数组中第一个元素。

而当路由切换时,上一个页面触发doUnbindEvent函数,新页面触发doBindEvent函数,而这两者是同时触发,导致一边向CACHE数组中添加绑定元素,一边从CACHE数组中移除元素。当一个元素绑定多个事件时,存在index为null,会移除新页面元素刚刚绑定的事件。导致新页面绑定事件失败。

所以问题原因在于产生了null,而作者没有对null这种类型拦截,所以只需将isNaN处的拦截换成null的拦截就可以。

对于上面移除CACHE中元素,作者用isNaN进行了拦截,我理解不了作者的意图(原谅我的无知),index的返回值应该只会为number类型或null,不会为NaN类型,希望作者解释下为什么这样用。

对于alloyFinger源码及vue版本的源码我抽时间读了一遍,并且都给了注释。在项目中使用alloyfinger遇到一些问题,对源码进行了改造,解决了几个问题:

1、源码中移动端是基于理想视口布局,实际项目中可能就不是理想视口,如淘宝的flexible,对视口进行了缩放,因此对于swipe事件界定的30px对于iphone和android手势滑动的距离是不同的。故对此进行了适配。

2、longTap事件微调,官方项目中触发longTap事件后会继续触发tap等事件,实际项目中longTap事件触发后可能就要阻止tap等相关事件。

3、阻止冒泡事件,由于其事件大部分是hack的,并不是原生事件,故阻止冒泡不能像原生那样进行,需要在touch-end里阻止冒泡,本方案提供了vue版本的自定义指令注入修饰符来阻止冒泡,如:v-finger:tap.stopPropagation。

能力有限,有什么问题欢迎拍砖。项目地址

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants