Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些

本篇文章給大家分享的是有關(guān)Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

“只有客戶(hù)發(fā)展了,才有我們的生存與發(fā)展!”這是成都創(chuàng)新互聯(lián)的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對(duì)成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無(wú)止境。

defineProperty 定義對(duì)象的屬性,只不過(guò)屬性里的get和set實(shí)現(xiàn)了響應(yīng)式。

常用:

  • value屬性值

  • get

  • set

  • writeable 是否可寫(xiě)

  • enumrable 可遍歷

Vue從改變一個(gè)數(shù)據(jù)到發(fā)生改變的過(guò)程

Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些

 Vue2.X數(shù)據(jù)響應(yīng)原理

創(chuàng)建頁(yè)面,實(shí)現(xiàn)延時(shí)2s修改對(duì)象的值。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>LearnVue3.0</title>
</head>
<body>
  <div id="app"></div>
  <script type="text/javascript" src="test.js"></script>
  <script type="text/javascript">
    const vm = new vue();
    setTimeout(function () {
      console.log('change');
      console.log(vm.$data);
      vm.$data.a = 444;
    }, 2000);

  </script>
</body>
</html>

defineProperty 實(shí)現(xiàn):

function vue() {
  this.$data = {
    a: 1
  };
  this.el = document.getElementById('app');
  this._html = "";
  this.observe(this.$data);
  this.render();
}

vue.prototype.observe = function (obj) {
  let self = this;
  let value;

  for (let key in obj) {
    value = obj[key];
    if (typeof value === 'object') {
      this.observe(value);
    } else {
      Object.defineProperty(this.$data, key, {
        get: function () {
          return value;
        },
        set: function (newvalue) {
          value = newvalue;
          self.render()
        }
      })
    }
  }
}

vue.prototype.render = function () {
  this._html = "I am " + this.$data.a;
  this.el.innerHTML = this._html;
}

在Chrome中console運(yùn)行,結(jié)果頁(yè)面顯示: I am 444

針對(duì)數(shù)組特性化處理:

let arraypro = Array.prototype;
// 為什么要create再次創(chuàng)建對(duì)象,create是深拷貝,不影響之前的arraypro
let arrayob = Object.create(arraypro);
// 定義哪些方法觸發(fā)更新
let arr = ["push", "pop", "shift"];

// arr里的方法,既能保持原有方法,又能觸發(fā)更新
// 裝飾者模式
arr.forEach(function (method, index) {
  // 對(duì)自己的push方法重寫(xiě)
  arrayob[method] = function () {
    let ret = arraypro[method].apply(this, arguments);
    // self.render();
    console.log('檢測(cè)到數(shù)組變化,觸發(fā)更新');
    return ret;
  }
});

在Chrome中console運(yùn)行示例:

let arr = [];
arr.__proto__ = arrayob;
arr.push(1);

結(jié)果顯示:

Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些 

Vue3.0數(shù)據(jù)響應(yīng)原理

Vue3.0數(shù)據(jù)響應(yīng)原理

創(chuàng)建頁(yè)面,實(shí)現(xiàn)延時(shí)2s修改對(duì)象的值。代碼同上。

Proxy實(shí)現(xiàn):

function vue() {
  this.$data = {
    a: 1
  };
  this.el = document.getElementById('app');
  this._html = "";
  this.observe(this.$data);
  this.render();
}

vue.prototype.observe = function (obj) {
  let self = this;

  this.$data = new Proxy(this.$data, {
    get: function (target, key) {
      return target[key];
    },
    set: function (target, key, newvalue) {
      target[key] = newvalue;
      self.render();
    }
  })
}

vue.prototype.render = function () {
  this._html = "I am " + this.$data.a;
  this.el.innerHTML = this._html;
}

在Chrome中console運(yùn)行,結(jié)果頁(yè)面顯示: I am 444

為什么改用Proxy

  • defineProperty只能監(jiān)聽(tīng)某個(gè)屬性,不能對(duì)全對(duì)象監(jiān)聽(tīng)

  • 可以省去for in循環(huán)提升效率

  • 可以監(jiān)聽(tīng)數(shù)組,不用再去單獨(dú)的對(duì)數(shù)組做特異性操作

Proxy還能做什么

校驗(yàn)類(lèi)型

function createValidator(target, validator) {
  return new Proxy(target, {
    _validator: validator,
    set(target, key, value, proxy) {
      if(target.hasOwnProperty(key)) {
        let validator = this._validator[key];
        if(validator(value)) {
          return Reflect.set(target, key, value, proxy);
        } else {
          throw Error('type error');
        }
      }
    }
  })
}

let personValidator = {
  name(val) {
    return typeof val === 'string';
  },
  age(val) {
    return typeof val === 'number' && val > 18;
  }
}

class person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
    return createValidator(this, personValidator);
  }
}

在Chrome中console運(yùn)行示例:

let tmp = new person('張三', 30);

結(jié)果顯示:

Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些

以上就是Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱(chēng)欄目:Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些
網(wǎng)址分享:http://muchs.cn/article2/jeppic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)網(wǎng)站策劃、Google、自適應(yīng)網(wǎng)站、定制網(wǎng)站網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)