Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),南川企業(yè)網(wǎng)站建設(shè),南川品牌網(wǎng)站建設(shè),網(wǎng)站定制,南川網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,南川網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

常用:

  • value屬性值

  • get

  • set

  • writeable 是否可寫

  • 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方法重寫
  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)聽某個(gè)屬性,不能對(duì)全對(duì)象監(jiān)聽

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

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

Proxy還能做什么

校驗(yàn)類型

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ì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:Vue2.X和Vue3.0數(shù)據(jù)響應(yīng)的區(qū)別有哪些-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)于:http://muchs.cn/article6/dcphig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、用戶體驗(yàn)、面包屑導(dǎo)航、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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)

h5響應(yīng)式網(wǎng)站建設(shè)