Vue2源码解析-双向绑定

两个文件,一个html,一个js。

<body>
    <div id="app">
        <h1>{{str}}</h1>
        <input type="text" v-model="str">
    </div>
</body>
<script src="./Vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            str: "你好"
        }
    })
</script>
class Vue {
    constructor(options) {
        this.$options = options;
        this.$watchEvent = {};
        this.$data = options.data;
        this.$el = document.querySelector(options.el);
        // 数据劫持
        this.proxyData()
        // 数据监听
        this.ovserve()
        // 编译模板
        this.compile(this.$el);
    }

    proxyData() {
        // 1.给Vue实例赋熟悉,来自于data
        // 2.data中的属性和Vue实例的属性是一一对应的(劫持)
        for (let key in this.$data) {
            Object.defineProperty(this, key, {
                get() {
                    return this.$data[key];
                },
                set(val) {
                    this.$data[key] = val;
                }
            })
        }
    }

    // 触发data中的数据发生变化来执行watch中的update
    ovserve() {
        for (let key in this.$data) {
            let value = this.$data[key];
            let that = this;
            Object.defineProperty(this.$data, key, {
                get() {
                    return value;
                },
                set(val) {
                    value = val;
                    if (that.$watchEvent[key]) {
                        that.$watchEvent[key].forEach((item, index) => {
                            item.update();
                        })
                    }
                }
            })
        }
    }

    compile(node) {
        node.childNodes.forEach((item, index) => {
            // 判断节点类型
            console.log(item);
            // 1===元素节点
            if (item.nodeType === 1) {
                // 判断是否绑定了v-model
                if (item.hasAttribute("v-model")) {
                    let vmKey = item.getAttribute("v-model").trim();
                    if (this.hasOwnProperty(vmKey)) {
                        item.value = this[vmKey];
                    }
                    item.addEventListener('input', (event) => {
                        this[vmKey] = event.target.value;
                    })
                }
                if (item.childNodes.length > 0) {
                    this.compile(item);
                }
            }
            // 2===属性节点
            else if (item.nodeType === 2) { }
            // 3===文本节点
            else if (item.nodeType === 3) {
                let reg = /\{\{(.*?)\}\}/g;
                let text = item.textContent;
                item.textContent = text.replace(reg, (match, key) => {
                    key = key.trim();
                    // 判断是否是data中的属性
                    if (this.hasOwnProperty(key)) {
                        let watcher = new Watche(this, key, item, "textContent")
                        // 检查当前对象是否已经有一个名为key的属性,用于存储观察者(watcher)列表
                        if (this.$watchEvent[key]) {
                            this.$watchEvent[key].push(watcher);
                        }
                        // 如果当前对象已经有一个名为key的属性,则将新创建的watcher添加到该属性中
                        else {
                            this.$watchEvent[key] = [];
                            this.$watchEvent[key].push(watcher);
                        }
                    }
                    return this.$data[key];
                })
            }
        })
    }
}

class Watche {
    /**
     * @function 构造函数 
     * @param {*} vm 对象
     * @param {*} key 属性名称
     * @param {*} node 节点
     * @param {*} attr 改变文本节点内容的字符串
     */
    constructor(vm, key, node, attr) {
        this.vm = vm;
        this.key = key;
        this.node = node;
        this.attr = attr;
    }

    update() {
        // 更新文本节点的内容
        this.node[this.attr] = this.vm.$data[this.key];
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值