栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Vue.js

vue双向绑定及观察者模式剖析

Vue.js 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue双向绑定及观察者模式剖析

在Vue2中,使用了Object.defineProterty()这个函数来实现双向绑定,这也就是为什么Vue不兼容IE8
1 响应式原理
让我们先从相应式原理开始。我们可以通过Object.defineProterty()来自定义Object的getter和setter 从而达到我们的目的。代码如下

function observe(value, cb) {
    Object.keys(value).forEach((key) => defineReactive(value, key, value[key] , cb))
}

function defineReactive (obj, key, val, cb) {
    Object.defineProperty(obj, key, {
 enumerable: true,
 configurable: true,
 get: ()=>{
     
     
     return val
 },
 set:newVal=> {
     val = newVal;
     cb();
 }
    })
}

class Vue {
    constructor(options) {
 this._data = options.data;
 observe(this._data, options.render)
    }
}

let app = new Vue({
    el: '#app',
    data: {
 text: 'text',
 text2: 'text2'
    },
    render(){
 console.log("render");
    }
})

通过observe函数对app.data上的每一个key和value都设定getter和setter。当value改变的时候触发setter,就会触发render这个函数。响应式的目的就达成,如果是视图更新的话我们通过监听dom的input事件来触发数据更新
但是现在我们只有在改变vue._data.text的时候才会触发他们的setter,但是我想偷懒,只改变vue.text就能触发到setter怎么做呢?
我们使用代理的方法

_proxy.call(this, options.data);


function _proxy (data) {
    const that = this;
    Object.keys(data).forEach(key => {
 Object.defineProperty(that, key, {
     configurable: true,
     enumerable: true,
     get: function proxyGetter () {
  return that._data[key];
     },
     set: function proxySetter (val) {
  that._data[key] = val;
     }
 })
    });
}

依赖收集
让我们再来看看下面的代码

new Vue({
    template: 
 `
     text1: {{text1}}
     text2: {{text2}}
 `,
    data: {
 text1: 'text1',
 text2: 'text2',
 text3: 'text3'
    }
});

当你的text3变化的时候,实际上text3并没有被渲染,但是也会触发一次render函数,这显然是不对的。所以我们需要收集依赖。
我们只需要在初始化的时候渲染一遍,那所有渲染所依赖的数据都会被触发getter,这时候我们只要把这个数据放到一个列表里就好啦!
我们先来认识一下Dep(dependencies)这个类,下图是一个最简单的Dep类。我们可以把他理解为发布者(这点很重要!!)

class Dep {
    constructor () {
 this.subs = [];
    }

    addSub (sub: Watcher) {
 this.subs.push(sub)
    }

    removeSub (sub: Watcher) {
 remove(this.subs, sub)
    }
    
    notify () {
 // stabilize the subscriber list first
 const subs = this.subs.slice()
 for (let i = 0, l = subs.length; i < l; i++) {
     subs[i].update()
 }
    }
}
function remove (arr, item) {
    if (arr.length) {
 const index = arr.indexOf(item)
 if (index > -1) {
     return arr.splice(index, 1)
    }
}

我们每次触发getter的时候,只要把触发的对象放到dep.sub里面就好啦!
但是现在问题来了,我们用什么来装这个触发的’对象’,也可以说式订阅者呢?
我们使用Watcher这个类

class Watcher {
    constructor (vm, expOrFn, cb, options) {
 this.cb = cb;
 this.vm = vm;

 
 Dep.target = this;
 
 
 this.cb.call(this.vm);
    }

    update () {
 this.cb.call(this.vm);
    }
}

vm即是vue实例, expOrFn就是{{a+b}}里面的a+b, cb就是回调函数就是return a+b, options是一些配置项。
Vue在第一次渲染列表的时候如果碰到{{xxx}}这样的表达式,就会new Watcher()。解析里面的函数,然后把当前的watcher实例赋给Dep.target(Dep.target是全局的,一次性只能有一个存在,因为Vue一次只处理一个依赖)。然后执行回调函数。(这里看似是执行回调函数渲染,其实又触发了一次getter,然后就会把当前的依赖添加到sub里去)
接下来开始依赖收集

class Vue {
    constructor(options) {
 this._data = options.data;
 observer(this._data, options.render);
 let watcher = new Watcher(this, );
    }
}

function defineReactive (obj, key, val, cb) {
    
    const dep = new Dep();

    Object.defineProperty(obj, key, {
 enumerable: true,
 configurable: true,
 get: ()=>{
     if (Dep.target) {
  
  dep.addSub(Dep.target);
     }
 },
 set:newVal=> {
     
     dep.notify();
 }
    })
}

Dep.target = null; //防止依赖重复添加

这儿我们通过示例来讲解