美文网首页
vue3.0 + ts 引入fastclick 与 ios下由f

vue3.0 + ts 引入fastclick 与 ios下由f

作者: seaflyj | 来源:发表于2020-11-26 16:41 被阅读0次

ios下由fastclick导致input框点击慢的问题 同样依然采用了vue3.0 + ts 架构

由于iphone上click事件会有300毫秒的延时,所以我们会使用fastclick插件来处理,但是后来发现这个插件在iphone上会有很多奇怪的问题,比如点击文件上传,会莫名的调用多次等等

解决方案

首先安装fastclick

npm install fastclick --save
// 或者(推荐)
cnpm install   fastclick --save

在main.ts中引入

//控制引入FastClick后input点击迟钝问题
import FastClick from "fastclick"
FastClick.prototype.focus = (targetElement: any) => {
  let length
  if (
    targetElement.setSelectionRange &&
    targetElement.type.indexOf("date") !== 0 &&
    targetElement.type !== "time" &&
    targetElement.type !== "month"
  ) {
    length = targetElement.value.length
    targetElement.focus()
    targetElement.setSelectionRange(length, length)
  }
}
FastClick.attach(document.body)

原本node-modules下面的@types/fastclick/index.d.ts的类型定义文件是有问题的,我记得是找不到attach这个方法,查找其它解决办法,好多人就建议直接修改@types/fastclick/index.d.ts文件的内容,但是大哥们,你们从新启动项目他都会还原为原有的数据的,所以我们应该直接删掉@types/fastclick/index.d.ts文件,在shims-vue.d.ts文件下,重新定义类型,于是;

// Type definitions for FastClick v1.0.3
// Project: https://github.com/ftlabs/fastclick
// Definitions by: Shinnosuke Watanabe <https://github.com/shinnn>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

interface FastClickObject {
  lastTouchIdentifier: number;
  layer: Element;
  tapDelay: number;
  targetElement: any;
  touchBoundary: number;
  touchStartX: number;
  touchStartY: number;
  trackingClick: boolean;
  trackingClickStart: number;
  destroy(): void;
  determineEventType(targetElement: any): string;
  findControl(labelElement: any /* EventTarget | HTMLLabelElement */): any;
  focus(targetElement: any /* EventTarget | Element */): void;
  getTargetElementFromEventTarget(eventTarget: EventTarget): any;
  needsClick(target: any /* EventTarget | Element */): boolean;
  needsFocus(target: any /* EventTarget | Element */): boolean;
}

interface FastClickOptions {
    touchBoundary?: number;
    tapDelay?: number;
}

interface FastClickStatic {
    new(layer: any, options?: FastClickOptions): FastClickObject;
    attach(layer: any, options?: FastClickOptions): FastClickObject;
}

declare module "fastclick" {
  var FastClick: FastClickStatic;//改成这行代码
    export = FastClick;
}

declare var FastClick: FastClickStatic;

没错,直接将这段代码复制过去就好了,如果不起作用,一定要手动删除原有的@types/fastclick/index.d.ts文件;

相关文章

网友评论

      本文标题:vue3.0 + ts 引入fastclick 与 ios下由f

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