問題總結(jié)

1. 取消axios請求

  • 業(yè)務(wù)場景:單頁應(yīng)用,希望退出當(dāng)前頁的時候,取消請求
  • 官網(wǎng): https://github.com/axios/axios
  • 方法:
const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.get('/user/12345', {
  cancelToken: source.token
}).catch(function (thrown) {
  if (axios.isCancel(thrown)) {
    console.log('Request canceled', thrown.message);
  } else {
    // handle error
  }
});

axios.post('/user/12345', {
  name: 'new name'
}, {
  cancelToken: source.token
})

// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');

You can also create a cancel token by passing an executor function to the CancelToken constructor:

const CancelToken = axios.CancelToken;
let cancel;

axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // An executor function receives a cancel function as a parameter
    cancel = c;
  })
});

// cancel the request
cancel();

Note: you can cancel several requests with the same cancel token.

2. input type=file 上傳圖片問題

(1) 多次上傳同一張圖片,且避免重復(fù)點(diǎn)擊觸發(fā)多次相冊

onInputClick(event) {
    // 可二次上傳同一張圖片
    event.target.value = null;
    // 避免連續(xù)點(diǎn)擊時重復(fù)打開相冊
    if (photoLoading) {
      event.preventDefault();
    }
    photoLoading = 1;
    setTimeout(function() {
      photoLoading = 0;
    }, 500);
  }

(2) <input type="file" accept="image/jpg,image/jpeg,image/png"/>點(diǎn)擊上傳圖片,會導(dǎo)致部分安卓設(shè)備黑屏并死機(jī)

  • 原因:不支持input的accept屬性
  • 方案:去除accept屬性<input type="file" />,目前遇到該問題的有: android 6.0.1

3. 獲取元素距離屏幕頂部的高度

var mTop = document.getElementById('fff').offsetTop;
    //減去滾動條的高度
    var sTop =  window.pageYOffset  //用于FF
    || document.documentElement.scrollTop  
    || document.body.scrollTop  
    || 0;
    var result = mTop - sTop;

4. css 設(shè)置iphonex 底部安全區(qū)域

  $safeArea:34px;
  //iphoneX、iphoneXs
  @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
    padding-bottom: $safeArea;
  }
 
  //iphone Xs Max
  @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:3) {
    padding-bottom: $safeArea;
  }
  
  //iphone XR
  @media only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio:2) {
    padding-bottom: $safeArea;
  }

5. 解決瀏覽器不支持跨域請求,控制臺執(zhí)行:

open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/your name/MyChromeDevUserData/

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

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

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