国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 web前端 js教程 使用Vue行動端如何實現(xiàn)圖片裁切組件

使用Vue行動端如何實現(xiàn)圖片裁切組件

Jun 23, 2018 pm 06:06 PM
vue

這篇文章給大家介紹了基於Vue的行動端圖片裁剪組件功能,因為移動端是用vue,所以就寫成了一個vue組件,下面就說說自己的一些實現(xiàn)思路,需要的朋友可以參考下

最近專案上要做一個車牌辨識的功能。本來以為很簡單,只要將圖片丟給後臺就可以了,但是經(jīng)測試後辨識率只有20-40%。因此產(chǎn)品建議拍攝圖片後,可以對圖片進行拖曳和縮放,然後裁剪車牌部分上傳給後臺來提高識別率。剛開始的話還是百度了一下看看有沒有現(xiàn)成的組件,但是找來找去都沒有找到一個合適的,還好這個功能不是很著急,因此自己週末就在家裡研究一下。

  Demo位址:https://vivialex.github.io/demo/imageClipper/index.html

  下載位址:https://github.com/vivialex/vue-imageClipper

#  因為行動端是用vue,所以就寫成了一個vue組件,下面就說說自己的一些實現(xiàn)思路(本人技術(shù)有限,各位大神請體諒。另外展示的代碼不一定是某個功能的完整程式碼),先看看效果: 

 ?

#  一、元件的初始化參數(shù)

## 1、圖片img(url或者base64 data-url)

  2、截圖的寬clipperImgWidth

  3、截圖的高clipperImgHeight

props: {
  img: String, //url或dataUrl
  clipperImgWidth: {
    type: Number,
    default: 500
  },
  clipperImgHeight: {
    type: Number,
    default: 200
  }
}
  二、佈局

  在Z軸方向看主要是由4層組成。第1層是一個佔滿整個容器的canvas(稱為cCanvas);第2層是一個有透明度的遮罩層;第3層是裁剪的區(qū)域(示例圖中的白色方框),裡麵包含一個與裁剪區(qū)域大小相等的canvas(稱為pCanvas);第4層是一個透明層gesture-mask,用作綁定touchstart,touchmove,touchend事件。其中兩個canvas都會載入同一張圖片,只是起始座標不一樣。為什麼需要兩個canvas?因為想做出當手指離開螢幕時,裁剪區(qū)域外的部分錶面會有一個遮罩層的效果,這樣能突出裁剪區(qū)域的內(nèi)容。

<p class="cut-container" ref="cut">
  <canvas ref="canvas"></canvas>
  <!-- 裁剪部分 -->
  <p class="cut-part">
    <p class="pCanvas-container">
      <canvas ref="pCanvas"></canvas>
    </p>
  </p>
  <!-- 底部操作欄 -->
  <p class="action-bar">
    <button class="btn-cancel" @click="_cancel">取消</button>
    <button class="btn-ok" @click="_cut">確認</button>
  </p>
  <!-- 背景遮罩 -->
  <p class="mask" :class="{opacity: maskShow}"></p>
  <!-- 手勢操作層 -->
  <p class="gesture-mask" ref="gesture"></p>
</p>
  三、初始化canvas

  canvas繪製的圖片在hdpi顯示器上會出現(xiàn)模糊,具體原因這裡不作分析,可以參考下這裡。我在這裡的做法是讓canvas的width與height為其css width/height的devicePixelRatio倍,以及呼叫canvas api時所傳入的參數(shù)都要乘以window.devicePixelRatio。最後也要記錄兩個canvas座標原點的x, y差值(originXDiff與originYDiff)。如下

_ratio(size) {
  return parseInt(window.devicePixelRatio * size);
},
_initCanvas() {
  let $canvas = this.$refs.canvas,
    $pCanvas = this.$refs.pCanvas,
    clipperClientRect = this.$refs.clipper.getBoundingClientRect(),
    clipperWidth = parseInt(this.clipperImgWidth / window.devicePixelRatio),
    clipperHeight = parseInt(this.clipperImgHeight / window.devicePixelRatio);

  this.ctx = $canvas.getContext(&#39;2d&#39;);
  this.pCtx = $pCanvas.getContext(&#39;2d&#39;);

  //判斷clipperWidth與clipperHeight有沒有超過容器值
  if (clipperWidth < 0 || clipperWidth > clipperClientRect.width) {
    clipperWidth = 250
  }

  if (clipperHeight < 0 || clipperHeight > clipperClientRect.height) {
    clipperHeight = 100
  }

  //因為canvas在手機上會被放大,因此里面的內(nèi)容會模糊,這里根據(jù)手機的devicePixelRatio來放大canvas,然后再通過設置css來收縮,因此關于canvas的所有值或坐標都要乘以devicePixelRatio
  $canvas.style.width = clipperClientRect.width + &#39;px&#39;;
  $canvas.style.height = clipperClientRect.height + &#39;px&#39;;
  $canvas.width = this._ratio(clipperClientRect.width);
  $canvas.height = this._ratio(clipperClientRect.height);

  $pCanvas.style.width = clipperWidth + &#39;px&#39;;
  $pCanvas.style.height = clipperHeight + &#39;px&#39;;
  $pCanvas.width = this._ratio(clipperWidth);
  $pCanvas.height = this._ratio(clipperHeight);

  //計算兩個canvas原點的x y差值
  let cClientRect = $canvas.getBoundingClientRect(),
    pClientRect = $pCanvas.getBoundingClientRect();

  this.originXDiff = pClientRect.left - cClientRect.left;
  this.originYDiff = pClientRect.top - cClientRect.top;
  this.cWidth = cClientRect.width;
  this.cHeight = cClientRect.height;
}
  四、載入圖片

  載入圖片比較簡單,首先是建立一個Image物件並監(jiān)聽器onload事件(因為載入的圖片有可能是跨域的,因此要設定其crossOrigin屬性為Anonymous,然後伺服器上要設定Access-Control-Allow-Origin回應頭)。載入的圖片如果寬高大於容器的寬高,要對其進行縮小處理。最後垂直水平居中顯示()(這裡注意的是要保存圖片繪製前的寬高值,因為日後縮放圖片是以該值為基礎再乘以縮放倍率,這裡取imgStartWidth,imgStartHeight)如下

_loadImg() {
  if (this.imgLoading || this.loadImgQueue.length === 0) {
    return;
  }
  let img = this.loadImgQueue.shift();
  if (!img) {
    return;
  }
  let $img = new Image(),
    onLoad = e => {
      $img.removeEventListener(&#39;load&#39;, onLoad, false);
      this.$img = $img;
      this.imgLoaded = true;
      this.imgLoading = false;
      this._initImg($img.width, $img.height);
      this.$emit(&#39;loadSuccess&#39;, e);
      this.$emit(&#39;loadComplete&#39;, e);
      this._loadImg();
    },
    onError = e => {
      $img.removeEventListener(&#39;error&#39;, onError, false);
      this.$img = $img = null;
      this.imgLoading = false;
      this.$emit(&#39;loadError&#39;, e);
      this.$emit(&#39;loadComplete&#39;, e);
      this._loadImg();
    };
  this.$emit(&#39;beforeLoad&#39;);
  this.imgLoading = true;
  this.imgLoaded = false;
  $img.src = this.img;
  $img.crossOrigin = &#39;Anonymous&#39;; //因為canvas toDataUrl不能操作未經(jīng)允許的跨域圖片,這需要服務器設置Access-Control-Allow-Origin頭
  $img.addEventListener(&#39;load&#39;, onLoad, false);
  $img.addEventListener(&#39;error&#39;, onError, false);
}
_initImg(w, h) {
  let eW = null,
    eH = null,
    maxW = this.cWidth,
    maxH = this.cHeight - this.actionBarHeight;
  //如果圖片的寬高都少于容器的寬高,則不做處理
  if (w <= maxW && h <= maxH) {
    eW = w;
    eH = h;
  } else if (w > maxW && h <= maxH) {
    eW = maxW;
    eH = parseInt(h / w * maxW);
  } else if (w <= maxW && h > maxH) {
    eW = parseInt(w / h * maxH);
    eH = maxH;
  } else {
    //判斷是橫圖還是豎圖
    if (h > w) {
      eW = parseInt(w / h * maxH);
      eH = maxH;
    } else {
      eW = maxW;
      eH = parseInt(h / w * maxW);
    }
  }
  if (eW <= maxW && eH <= maxH) {
    //記錄其初始化的寬高,日后的縮放功能以此值為基礎
    this.imgStartWidth = eW;
    this.imgStartHeight = eH;
    this._drawImage((maxW - eW) / 2, (maxH - eH) / 2, eW, eH);
  } else {
    this._initImg(eW, eH);
  }
}

  

五、繪製圖片

  下面的_drawImage有四個參數(shù),分別是圖片對應cCanvas的x,y座標以及圖片目前的寬高w ,h。函數(shù)首先會清空兩個canvas的內(nèi)容,方法是重新設定canvas的寬高。然後更新元件實例中對應的值,最後再呼叫兩個canvas的drawImage去繪製圖片。對於pCanvas來說,其繪製的圖片座標值為x,y減去對應的originXDiff與originYDiff(其實相當於切換座標系顯示而已,因此只需要減去兩個座標係原點的x,y差值即可)??纯闯淌酱a

_drawImage(x, y, w, h) {
  this._clearCanvas();
  this.imgX = parseInt(x);
  this.imgY = parseInt(y);
  this.imgCurrentWidth = parseInt(w);
  this.imgCurrentHeight = parseInt(h);
  //更新canvas
  this.ctx.drawImage(this.$img, this._ratio(x), this._ratio(y), this._ratio(w), this._ratio(h));
  //更新pCanvas,只需要減去兩個canvas坐標原點對應的差值即可
  this.pCtx.drawImage(this.$img, this._ratio(x - this.originXDiff), this._ratio(y - this.originYDiff), this._ratio(w), this._ratio(h));
},
_clearCanvas() {
  let $canvas = this.$refs.canvas,
    $pCanvas = this.$refs.pCanvas;
  $canvas.width = $canvas.width;
  $canvas.height = $canvas.height;
  $pCanvas.width = $pCanvas.width;
  $pCanvas.height = $pCanvas.height;
}

  

### 六、行動圖片############  行動圖片實作非常簡單,先給gesture-mask綁定touchstart,touchmove,touchend事件,以下分別介紹這三個事件的內(nèi)容######  先定義四個變數(shù)scx, scy(手指的起始座標),iX,iY(圖片目前的座標,相對於cCanvas)。 ######  1、touchstart######    方法很簡單,就是取得touches[0]的pageX,pageY來更新scx與scy以及更新iX與iY######  2、touchmove##2、touchmove## ####    取得touches[0]的pageX,宣告變數(shù)f1x存放,移動後的x座標等於iX f1x - scx,y座標同理,最後呼叫_drawImage來更新圖片。 ######  看看程式碼吧###
_initEvent() {
  let $gesture = this.$refs.gesture,
    scx = 0,
    scy = 0;
  let iX = this.imgX,
    iY = this.imgY;
  $gesture.addEventListener(&#39;touchstart&#39;, e => {
    if (!this.imgLoaded) {
      return;
    }
    let finger = e.touches[0];
      scx = finger.pageX;
      scy = finger.pageY;
      iX = this.imgX;
      iY = this.imgY;  
  }, false);
  $gesture.addEventListener(&#39;touchmove&#39;, e => {
    e.preventDefault();
    if (!this.imgLoaded) {
      return;
    }
    let f1x = e.touches[0].pageX,
      f1y = e.touches[0].pageY;
      this._drawImage(iX + f1x - scx, iY + f1y - scy, this.imgCurrentWidth, this.imgCurrentHeight);
  }, false);
}

   七、縮放圖片(這里不作特別說明的坐標都是相對于cCanvas坐標系)

  繪制縮放后的圖片無非需要4個參數(shù),縮放后圖片左上角的坐標以及寬高。求寬高相對好辦,寬高等于imgStartWidth * 縮放比率與imgstartHeight * 縮放倍率(imgStartWidth ,imgstartHeight 上文第四節(jié)有提到)。接下來就是求縮放倍率的問題了,首先在touchstart事件上求取兩手指間的距離d1;然后在touchmove事件上繼續(xù)求取兩手指間的距離d2,當前縮放倍率= 初始縮放倍率 + (d2-d1) / 步長(例如每60px算0.1),touchend事件上讓初始縮放倍率=當前縮放倍率。

  至于如何求取縮放后圖片左上角的坐標值,在草稿紙上畫來畫去,畫了很久......終于有點眉目。首先要找到一個縮放中心(這里做法是取雙指的中點坐標,但是這個坐標必須要位于圖片上,如果不在圖片上,則取圖片上離該中點坐標最近的點),然后存在下面這個等式

 ?。s放中心x坐標 - 縮放后圖片左上角x坐標)/ 縮放后圖片的寬度 = (縮放中心x坐標 - 縮放前圖片左上角x坐標)/ 縮放前圖片的寬度;(y坐標同理)

  接下來看看下面這個例子(在visio找了很久都沒有畫坐標系的功能,所以只能手工畫了)

  

  綠色框是一張10*5的圖片,藍色框是寬高放大兩倍后的圖片20*10,根據(jù)上面的公式推算的x2 = sx - w2(sx - x1) / w1,y2 = sy - h2(sy - y1) / h1。

  堅持...繼續(xù)看看代碼吧

_initEvent() {
  let $gesture = this.$refs.gesture,
    cClientRect = this.$refs.canvas.getBoundingClientRect(),
    scx = 0, //對于單手操作是移動的起點坐標,對于縮放是圖片距離兩手指的中點最近的圖標。
    scy = 0,
    fingers = {}; //記錄當前有多少只手指在觸控屏幕
  //one finger
  let iX = this.imgX,
    iY = this.imgY;
  //two finger
  let figureDistance = 0,
    pinchScale = this.imgScale;
  $gesture.addEventListener(&#39;touchstart&#39;, e => {
    if (!this.imgLoaded) {
      return;
    }
    if (e.touches.length === 1) {
      let finger = e.touches[0];
      scx = finger.pageX;
      scy = finger.pageY;
      iX = this.imgX;
      iY = this.imgY;
      fingers[finger.identifier] = finger;
    } else if (e.touches.length === 2) {
      let finger1 = e.touches[0],
        finger2 = e.touches[1],
        f1x = finger1.pageX - cClientRect.left,
        f1y = finger1.pageY - cClientRect.top,
        f2x = finger2.pageX - cClientRect.left,
        f2y = finger2.pageY - cClientRect.top;
      scx = parseInt((f1x + f2x) / 2);
      scy = parseInt((f1y + f2y) / 2);
      figureDistance = this._pointDistance(f1x, f1y, f2x, f2y);
      fingers[finger1.identifier] = finger1;
      fingers[finger2.identifier] = finger2;
      //判斷變換中點是否在圖片中,如果不是則去離圖片最近的點
      if (scx < this.imgX) {
        scx = this.imgX;
      }
      if (scx > this.imgX + this.imgCurrentWidth) {
        scx = this.imgX + this.imgCurrentHeight;
      }
      if (scy < this.imgY) {
        scy = this.imgY;
      }
      if (scy > this.imgY + this.imgCurrentHeight) {
        scy = this.imgY + this.imgCurrentHeight;
      }
    }
  }, false);
  $gesture.addEventListener(&#39;touchmove&#39;, e => {
    e.preventDefault();
    if (!this.imgLoaded) {
      return;
    }
    this.maskShowTimer && clearTimeout(this.maskShowTimer);
    this.maskShow = false;
    if (e.touches.length === 1) {
      let f1x = e.touches[0].pageX,
        f1y = e.touches[0].pageY;
      this._drawImage(iX + f1x - scx, iY + f1y - scy, this.imgCurrentWidth, this.imgCurrentHeight);
    } else if (e.touches.length === 2) {
      let finger1 = e.touches[0],
        finger2 = e.touches[1],
        f1x = finger1.pageX - cClientRect.left,
        f1y = finger1.pageY - cClientRect.top,
        f2x = finger2.pageX - cClientRect.left,
        f2y = finger2.pageY - cClientRect.top,
        newFigureDistance = this._pointDistance(f1x, f1y, f2x, f2y),
        scale = this.imgScale + parseFloat(((newFigureDistance - figureDistance) / this.imgScaleStep).toFixed(1));
      fingers[finger1.identifier] = finger1;
      fingers[finger2.identifier] = finger2;
      if (scale !== pinchScale) {
        //目前縮放的最小比例是1,最大是5
        if (scale < this.imgMinScale) {
          scale = this.imgMinScale;
        } else if (scale > this.imgMaxScale) {
          scale = this.imgMaxScale;
        }
        pinchScale = scale;
        this._scale(scx, scy, scale);
      }
    }
  }, false);
  $gesture.addEventListener(&#39;touchend&#39;, e => {
    if (!this.imgLoaded) {
      return;
    }
    this.imgScale = pinchScale;
    //從finger刪除已經(jīng)離開的手指
    let touches = Array.prototype.slice.call(e.changedTouches, 0);
    touches.forEach(item => {
      delete fingers[item.identifier];
    });
    //迭代fingers,如果存在finger則更新scx,scy,iX,iY,因為可能縮放后立即單指拖動
    let i,
      fingerArr = [];
    for(i in fingers) {
      if (fingers.hasOwnProperty(i)) {
        fingerArr.push(fingers[i]);
      }
    }
    if (fingerArr.length > 0) {
      scx = fingerArr[0].pageX;
      scy = fingerArr[0].pageY;
      iX = this.imgX;
      iY = this.imgY;
    } else {
      this.maskShowTimer = setTimeout(() => {
        this.maskShow = true;
      }, 300);
    }
    //做邊界值檢測
    let x = this.imgX,
      y = this.imgY,
      pClientRect = this.$refs.pCanvas.getBoundingClientRect();
    if (x > pClientRect.left + pClientRect.width) {
      x = pClientRect.left
    } else if (x + this.imgCurrentWidth < pClientRect.left) {
      x = pClientRect.left + pClientRect.width - this.imgCurrentWidth;
    }
    if (y > pClientRect.top + pClientRect.height) {
      y = pClientRect.top;
    } else if (y + this.imgCurrentHeight < pClientRect.top) {
      y = pClientRect.top + pClientRect.height - this.imgCurrentHeight;
    }
    if (this.imgX !== x || this.imgY !== y) {
      this._drawImage(x, y, this.imgCurrentWidth, this.imgCurrentHeight);
    }
  });
},
_scale(x, y, scale) {
  let newPicWidth = parseInt(this.imgStartWidth * scale),
    newPicHeight = parseInt(this.imgStartHeight * scale),
    newIX = parseInt(x - newPicWidth * (x - this.imgX) / this.imgCurrentWidth),
    newIY = parseInt(y - newPicHeight * (y - this.imgY) / this.imgCurrentHeight);
  this._drawImage(newIX, newIY, newPicWidth, newPicHeight);
},
_pointDistance(x1, y1, x2, y2) {
  return parseInt(Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
}

  說明一下fingers是干嘛的,是用來記錄當前有多少只手指在屏幕上觸摸??赡軙霈F(xiàn)這種情況,雙指縮放后,其中一只手指移出顯示屏,而另外一個手指在顯示屏上移動。針對這種情況,要在touchend事件上根據(jù)e.changedTouches來移除fingers里已經(jīng)離開顯示屏的finger,如果此時fingers里只剩下一個finger,則更新scx,scy,iX,iY為移動圖片做初始化準備。

  八、裁剪圖片

  這里很簡單,就調(diào)用pCanvas的toDataURL方法就可以了

_clipper() {
  let imgData = null;
  try {
    imgData = this.$refs.pCanvas.toDataURL();
  } catch (e) {
    console.error(&#39;請在response header加上Access-Control-Allow-Origin,否則canvas無法裁剪未經(jīng)許可的跨域圖片&#39;);
  }
  this.$emit(&#39;sure&#39;, imgData);
}

上面是我整理給大家的,希望今后會對大家有幫助。

相關文章:

在Bootstrap框架里使用treeview如何實現(xiàn)動態(tài)加載數(shù)據(jù)

在Nginx中如何配置多站點vhost

在vue中如何實現(xiàn)跳轉(zhuǎn)到之前頁面

在express+mockjs中如何實現(xiàn)后臺數(shù)據(jù)發(fā)送

以上是使用Vue行動端如何實現(xiàn)圖片裁切組件的詳細內(nèi)容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
怎樣開發(fā)一個完整的PythonWeb應用程序? 怎樣開發(fā)一個完整的PythonWeb應用程序? May 23, 2025 pm 10:39 PM

要開發(fā)一個完整的PythonWeb應用程序,應遵循以下步驟:1.選擇合適的框架,如Django或Flask。 2.集成數(shù)據(jù)庫,使用ORM如SQLAlchemy。 3.設計前端,使用Vue或React。 4.進行測試,使用pytest或unittest。 5.部署應用,使用Docker和平臺如Heroku或AWS。通過這些步驟,可以構(gòu)建出功能強大且高效的Web應用。

前端路由(Vue Router、React Router)的工作原理及配置方法? 前端路由(Vue Router、React Router)的工作原理及配置方法? May 20, 2025 pm 07:18 PM

前端路由系統(tǒng)的核心是將URL映射到組件,VueRouter和ReactRouter通過監(jiān)聽URL變化並加載相應組件實現(xiàn)無刷新頁面切換。配置方法包括:1.嵌套路由,允許在父組件中嵌套子組件;2.動態(tài)路由,根據(jù)URL參數(shù)加載不同組件;3.路由守衛(wèi),在路由切換前後執(zhí)行邏輯如權(quán)限檢查。

Vue的反應性轉(zhuǎn)換(實驗,然後被刪除)的意義是什麼? Vue的反應性轉(zhuǎn)換(實驗,然後被刪除)的意義是什麼? Jun 20, 2025 am 01:01 AM

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

Vue.js 與 React 在組件化開發(fā)中的核心差異是什麼? Vue.js 與 React 在組件化開發(fā)中的核心差異是什麼? May 21, 2025 pm 08:39 PM

Vue.js和React在組件化開發(fā)中的核心差異在於:1)Vue.js使用模板語法和選項式API,而React使用JSX和函數(shù)式組件;2)Vue.js採用響應式系統(tǒng),React則使用不可變數(shù)據(jù)和虛擬DOM;3)Vue.js提供多個生命週期鉤子,React則更多使用useEffect鉤子。

如何在VUE應用程序中實施國際化(I18N)和本地化(L10N)? 如何在VUE應用程序中實施國際化(I18N)和本地化(L10N)? Jun 20, 2025 am 01:00 AM

國際化和傾斜度invueAppsareprimandermedusingthevuei18nplugin.1.installvue-i18nvianpmoryarn.2.createlo calejsonfiles(例如,en.json,es.json)fortranslationMessages.3.setupthei18ninstanceinmain.jswithlocaleconfigurationandmessagefil

Vue 響應式原理及在數(shù)組更新時不觸發(fā)視圖更新的解決方案? Vue 響應式原理及在數(shù)組更新時不觸發(fā)視圖更新的解決方案? May 20, 2025 pm 06:54 PM

Vue.js處理數(shù)組更新時,視圖未更新是因為Object.defineProperty無法直接監(jiān)聽到數(shù)組變化。解決方法包括:1.使用Vue.set方法修改數(shù)組索引;2.重新賦值整個數(shù)組;3.使用Vue重寫過的變異方法操作數(shù)組。

使用VUE中的V-For指令使用關鍵屬性(:key)的好處??是什麼? 使用VUE中的V-For指令使用關鍵屬性(:key)的好處??是什麼? Jun 08, 2025 am 12:14 AM

Usingthe:keyattributewithv-forinVueisessentialforperformanceandcorrectbehavior.First,ithelpsVuetrackeachelementefficientlybyenablingthevirtualDOMdiffingalgorithmtoidentifyandupdateonlywhat’snecessary.Second,itpreservescomponentstateinsideloops,ensuri

您如何優(yōu)化VUE中大型列表或複雜組件的重新渲染? 您如何優(yōu)化VUE中大型列表或複雜組件的重新渲染? Jun 07, 2025 am 12:14 AM

優(yōu)化Vue中大型列表和復雜組件性能的方法包括:1.使用v-once指令處理靜態(tài)內(nèi)容,減少不必要的更新;2.實現(xiàn)虛擬滾動,僅渲染可視區(qū)域的內(nèi)容,如使用vue-virtual-scroller庫;3.通過keep-alive或v-once緩存組件,避免重複掛載;4.利用計算屬性和偵聽器優(yōu)化響應式邏輯,減少重渲染範圍;5.遵循最佳實踐,如在v-for中使用唯一key、避免模板中的內(nèi)聯(lián)函數(shù),並使用性能分析工具定位瓶頸。這些策略能有效提升應用流暢度。

See all articles