欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue之Dep和Observer的用法及說明

 更新時(shí)間:2023年05月17日 09:04:15   作者:陸小森_紅齊飄飄  
這篇文章主要介紹了Vue之Dep和Observer的用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vue 中響應(yīng)式系統(tǒng)利用了訂閱發(fā)布模式來實(shí)現(xiàn)基本的邏輯。本文將介紹其中的兩個(gè)重要角色,他們就是Dep和Observer。其中Observer 是觀察者和 Dep是訂閱收集和發(fā)布者。另外watcher是作為訂閱者的角色。

本文將重點(diǎn)將Observer和Dep。

一:Observer

vue 通過Observer 構(gòu)造函數(shù),為響應(yīng)式變量添加訪問和賦值的get set的回調(diào)。

? var Observer = function Observer (value) {
? ? this.value = value;
? ? this.dep = new Dep();
? ? this.vmCount = 0;
? ? def(value, '__ob__', this);
? ? if (Array.isArray(value)) {
? ? ? if (hasProto) {
? ? ? ? protoAugment(value, arrayMethods);
? ? ? } else {
? ? ? ? copyAugment(value, arrayMethods, arrayKeys);
? ? ? }
? ? ? this.observeArray(value);
? ? } else {
? ? ? this.walk(value); // 這是非數(shù)組值添加get set 回調(diào)
? ? }
? };

批量為obj上的key添加get set的回調(diào)

? Observer.prototype.walk = function walk (obj) {
? ? var keys = Object.keys(obj);
? ? for (var i = 0; i < keys.length; i++) {
? ? ? defineReactive$$1(obj, keys[i]);
? ? }
? };
// 這個(gè)是為obj上的key添加get set方法的核心邏輯
? function defineReactive$$1 (
? ? obj,
? ? key,
? ? val,
? ? customSetter,
? ? shallow
? ) {
? ? var dep = new Dep();
? ? var property = Object.getOwnPropertyDescriptor(obj, key);
? ? if (property && property.configurable === false) {
? ? ? return
? ? }
? ? // cater for pre-defined getter/setters
? ? var getter = property && property.get;
? ? var setter = property && property.set;
? ? if ((!getter || setter) && arguments.length === 2) {
? ? ? val = obj[key];
? ? }
? ? var childOb = !shallow && observe(val);
? ? Object.defineProperty(obj, key, {
? ? ? enumerable: true,
? ? ? configurable: true,
? ? ? get: function reactiveGetter () {
? ? ? ? ?// 省略N行
? ? ? },
? ? ? set: function reactiveSetter (newVal) {
? ? ? ?// 省略N行
? ? ? }
? ? });
? }

有了這一層,當(dāng)數(shù)據(jù)獲取和修改就會(huì)觸發(fā)這里的get 和 set

二:Dep 依賴關(guān)系

變量能夠響應(yīng)數(shù)據(jù)的獲取和修改對于一個(gè)透明處理dom更新的框架來說是不夠的,框架還需要處理變量和更新dom的watcher的依賴關(guān)系。Dep就是為了干這個(gè)事情的。

在之前的文章里,介紹過Watcher,vue中的組件在掛載前,都會(huì)基于組件的render函數(shù),生成一個(gè)Watcher實(shí)例,并執(zhí)行render函數(shù)來進(jìn)行渲染

渲染dom的時(shí)候我們需要讀取變量,這個(gè)時(shí)候就會(huì)觸發(fā)我們上一步Observer為每個(gè)變量里量身定做的get方法。那vue在get里做了什么事情呢?

請看下面一段代碼中的中文注釋:

function defineReactive$$1 (
? ? obj,
? ? key,
? ? val,
? ? customSetter,
? ? shallow
? ) {
? ? var dep = new Dep(); // 創(chuàng)建一個(gè)Dep實(shí)例
? ? var property = Object.getOwnPropertyDescriptor(obj, key);
? ? if (property && property.configurable === false) {
? ? ? return
? ? }
? ? // cater for pre-defined getter/setters
? ? var getter = property && property.get;
? ? var setter = property && property.set;
? ? if ((!getter || setter) && arguments.length === 2) {
? ? ? val = obj[key];
? ? }
? ? var childOb = !shallow && observe(val);
? ? Object.defineProperty(obj, key, {
? ? ? enumerable: true,
? ? ? configurable: true,
? ? ? get: function reactiveGetter () {
? ? ? ? var value = getter ? getter.call(obj) : val;
? ? ? ? if (Dep.target) {
? ? ? ? ? //在此處將當(dāng)前watcher加入到dep中
? ? ? ? ? dep.depend();?
? ? ? ? ? if (childOb) {
? ? ? ? ? ? childOb.dep.depend();
? ? ? ? ? ? if (Array.isArray(value)) {
? ? ? ? ? ? ? dependArray(value);
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? ? return value
? ? ? },

此處有一個(gè)Dep.target 存在的條件判斷,什么是Dep.target

Dep.target = null;

當(dāng)watcher實(shí)例創(chuàng)建完,默認(rèn)回去調(diào)用get方法來渲染組件界面

這個(gè)時(shí)候會(huì)通過pushTarget(this) 將當(dāng)前watcher設(shè)置到Dep.target上去。

? Watcher.prototype.get = function get () {
? ?// 設(shè)置當(dāng)前watcher到全局變量上去
? ? pushTarget(this);
? ? var value;
? ? // 省略N行代碼
? ? return value
? };

pushTarget的具體實(shí)現(xiàn)

?function pushTarget (target) {
? ? targetStack.push(target);
? ? Dep.target = target;
? }

這一步完成后,在調(diào)用render函數(shù)觸發(fā)dom中響應(yīng)式變量的get的時(shí)候

下面的代碼就能夠執(zhí)行了

?if (Dep.target) {
? ? ? ? ? //在此處將當(dāng)前watcher加入到dep中
? ? ? ? ? dep.depend();?

dep.depend 干了啥呢?

接著看:

?Dep.prototype.depend = function depend () {
? ? // Dep.target 對應(yīng)的就是上面的全局變量里的watcher
? ? if (Dep.target) {
? ? ? Dep.target.addDep(this);
? ? }
? };

也就是調(diào)用了watcher.addDep(this)

? Watcher.prototype.addDep = function addDep (dep) {
? ? var id = dep.id;
? ? if (!this.newDepIds.has(id)) {
? ? ? this.newDepIds.add(id);
? ? ? // ?會(huì)在watcher的實(shí)際例子里維護(hù)一個(gè)所以訂閱的dep的數(shù)組
? ? ? this.newDeps.push(dep);?
? ? ? if (!this.depIds.has(id)) {
? ? ? ? dep.addSub(this);?
? ? ? }
? ? }
? };
? Dep.prototype.addSub = function addSub (sub) {
? // 繞了一大圈,最終就是把watcher 塞到dep實(shí)例的subs數(shù)組里了。
? ? this.subs.push(sub);
? };

到此,dom的渲染訂閱者和數(shù)據(jù)的觀察發(fā)布者就關(guān)聯(lián)上了。

等用戶修改數(shù)據(jù)的時(shí)候,觸發(fā)為響應(yīng)數(shù)據(jù)設(shè)計(jì)的set 回調(diào)函數(shù)

set: function reactiveSetter (newVal) {
? ? ? ? var value = getter ? getter.call(obj) : val;
? ? ? ? /* eslint-disable no-self-compare */
? ? ? ? if (newVal === value || (newVal !== newVal && value !== value)) {
? ? ? ? ? return
? ? ? ? }
? ? ? ? /* eslint-enable no-self-compare */
? ? ? ? if (customSetter) {
? ? ? ? ? customSetter();
? ? ? ? }
? ? ? ? // #7981: for accessor properties without setter
? ? ? ? if (getter && !setter) { return }
? ? ? ? if (setter) {
? ? ? ? ? setter.call(obj, newVal);
? ? ? ? } else {
? ? ? ? ? val = newVal;
? ? ? ? }
? ? ? ? childOb = !shallow && observe(newVal);
? ? ? ? // 重點(diǎn)看這里,這里是發(fā)布者發(fā)布數(shù)據(jù)變化的消息
? ? ? ? // dep通知所有依賴的watcher
? ? ? ? dep.notify();
? ? ? }
? ? });

dep.notify做了啥事呢?

? Dep.prototype.notify = function notify () {
? ? // stabilize the subscriber list first
? ? // subs就是上面get回調(diào)里,我們用來加入watcher依賴的數(shù)組
? ? var subs = this.subs.slice();?
? ? if (!config.async) {
? ? ? // subs aren't sorted in scheduler if not running async
? ? ? // we need to sort them now to make sure they fire in correct
? ? ? // order
? ? ? subs.sort(function (a, b) { return a.id - b.id; });
? ? }
? ? for (var i = 0, l = subs.length; i < l; i++) {
? ? // ?遍歷調(diào)用watcher的update方法
? ? ? subs[i].update();?
? ? }
? };

Dep 的構(gòu)造函數(shù)和其他實(shí)例方法:

? // Dep的構(gòu)造函數(shù)
? var Dep = function Dep () {
? ? this.id = uid++;
? ? this.subs = [];
? };
// ?取消訂閱
? Dep.prototype.removeSub = function removeSub (sub) {
? ? remove(this.subs, sub);
? };

其他相關(guān)

// 移除deps數(shù)組里的不在newDeps里的老的依賴,將之前newDeps收集的dep依賴丟給this.deps
? Watcher.prototype.cleanupDeps = function cleanupDeps () {
? ? var i = this.deps.length;
? ? while (i--) {
? ? ? var dep = this.deps[i];
? ? ? if (!this.newDepIds.has(dep.id)) {
? ? ? ? dep.removeSub(this);
? ? ? }
? ? }
? ? var tmp = this.depIds;
? ? this.depIds = this.newDepIds;
? ? this.newDepIds = tmp;
? ? this.newDepIds.clear();
? ? tmp = this.deps;
? ? this.deps = this.newDeps;
? ? this.newDeps = tmp;
? ? this.newDeps.length = 0;
? };

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3路由新玩法useRoute和useRouter詳解

    vue3路由新玩法useRoute和useRouter詳解

    這篇文章主要介紹了vue3路由新玩法useRoute和useRouter,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue3 pinia使用及持久化注冊

    vue3 pinia使用及持久化注冊

    本文介紹了Pinia的使用方法及如何實(shí)現(xiàn)狀態(tài)持久化存儲(chǔ),首先,介紹了Pinia的安裝和在main.ts中的掛載,介紹了getters和actions的使用方法,最后,詳細(xì)說明了如何通過Pinia-plugin-persistedstate插件實(shí)現(xiàn)Pinia狀態(tài)的持久化處理,包括插件的安裝、配置和在main.ts文件中的注冊
    2024-10-10
  • vue實(shí)現(xiàn)幾秒后跳轉(zhuǎn)新頁面代碼

    vue實(shí)現(xiàn)幾秒后跳轉(zhuǎn)新頁面代碼

    這篇文章主要介紹了vue實(shí)現(xiàn)幾秒后跳轉(zhuǎn)新頁面代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁功能(附demo)

    vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁功能(附demo)

    這篇文章主要介紹了vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁功能(附demo),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。
    2017-03-03
  • 35個(gè)最好用的Vue開源庫(史上最全)

    35個(gè)最好用的Vue開源庫(史上最全)

    無論是開發(fā)新手還是經(jīng)驗(yàn)豐富的老手,我們都喜歡開源軟件包。本文主要介紹了35個(gè)最好用的Vue開源庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • vue 授權(quán)獲取微信openId操作

    vue 授權(quán)獲取微信openId操作

    這篇文章主要介紹了vue 授權(quán)獲取微信openId操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決

    Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決

    這篇文章主要介紹了Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue.js綁定class和style樣式(6)

    vue.js綁定class和style樣式(6)

    這篇文章我們將一起學(xué)習(xí)vue.js實(shí)現(xiàn)綁定class和style樣式,感興趣的小伙伴們可以參考一下
    2016-12-12
  • vue實(shí)現(xiàn)橫向斜切柱狀圖

    vue實(shí)現(xiàn)橫向斜切柱狀圖

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)橫向斜切柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue使用csp的簡單示例

    vue使用csp的簡單示例

    Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式框架,與其它大型框架不同的是,Vue被設(shè)計(jì)為可以自底向上逐層應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue使用csp的相關(guān)資料,需要的朋友可以參考下
    2022-08-08

最新評(píng)論