vue3.0 + ts 引入fastclick 與 ios下由fastclick導(dǎo)致input框點(diǎn)擊慢的問(wèn)題

ios下由fastclick導(dǎo)致input框點(diǎn)擊慢的問(wèn)題 同樣依然采用了vue3.0 + ts 架構(gòu)

由于iphone上click事件會(huì)有300毫秒的延時(shí),所以我們會(huì)使用fastclick插件來(lái)處理,但是后來(lái)發(fā)現(xiàn)這個(gè)插件在iphone上會(huì)有很多奇怪的問(wèn)題,比如點(diǎn)擊文件上傳,會(huì)莫名的調(diào)用多次等等

解決方案

首先安裝fastclick

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

在main.ts中引入

//控制引入FastClick后input點(diǎn)擊遲鈍問(wèn)題
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的類(lèi)型定義文件是有問(wèn)題的,我記得是找不到attach這個(gè)方法,查找其它解決辦法,好多人就建議直接修改@types/fastclick/index.d.ts文件的內(nèi)容,但是大哥們,你們從新啟動(dòng)項(xiàng)目他都會(huì)還原為原有的數(shù)據(jù)的,所以我們應(yīng)該直接刪掉@types/fastclick/index.d.ts文件,在shims-vue.d.ts文件下,重新定義類(lèi)型,于是;

// 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;

沒(méi)錯(cuò),直接將這段代碼復(fù)制過(guò)去就好了,如果不起作用,一定要手動(dòng)刪除原有的@types/fastclick/index.d.ts文件;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容