Giter VIP home page Giter VIP logo

-vue-'s People

Contributors

yangkuo1993 avatar

Watchers

 avatar  avatar

-vue-'s Issues

浅析vue原理

浅析vue原理

网上看了好多关于vue的文章,对vue的剖析基本一致。有些过于繁琐,让人懵懂。基于http://www.cnblogs.com/kidney/p/6052935.html?utm_source=gold_browser_extension这篇博文,自己动手实践了一下,就自己的理解来阐述这段代码。

观察者模式

function observe(obj, vm) { 
    Object.keys(obj).forEach(function (key) { 
        defineReactive(vm, key, obj[key]) 
    }) 
}   

访问器属性

function defineReactive(obj, key, val) {
    var dep = new Dep();
    Object.defineProperty(obj, key, {
        get: function () {
            if (Dep.target) dep.addSub(Dep.target)
            return val
        },
        set: function (newValue) {
            if (newValue === val) return
            val = newValue
            console.log('值是这么变化的,正确的值是' + val)
            dep.notify()
        }
    })
}

订阅发布模式

function Dep() {
    this.subs = []
}
Dep.prototype = {
    addSub: function (sub) {
        this.subs.push(sub);
    },
    notify: function () {
        this.subs.forEach(function (sub) {
            sub.update();
        })
    }
}

文档拦截

function nodeToFragment(node, vm) {
    var flag = document.createDocumentFragment();
    var child;
    while (child = node.firstChild) {
        compile(child, vm);
        flag.appendChild(child)
    }
    return flag;
}

编译

function compile(node, vm) {
    var reg = /\{\{(.*)\}\}/;
    if (node.nodeType === 1) {
        var attr = node.attributes;
        for(var i = 0; i < attr.length; i++) {
            if (attr[i].nodeName == 'v-model') {
                var name = attr[i].nodeValue;
                node.addEventListener('input', function (e) {
                    vm[name] = e.target.value
                })
                node.value = vm[name];
                node.removeAttribute('v-model');
            }
        }
    }
    if (node.nodeType === 3) {
        if (reg.test(node.nodeValue)) {
            var name = RegExp.$1;
            name = name.trim();
            // node.nodeValue = vm[name];
            new Watcher(vm, node, name)
        }
    }
}

监听数据变化文本响应

function Watcher(vm, node, name) {
    Dep.target = this;
    this.name = name;
    this.node = node;
    this.vm = vm;
    this.update();
    Dep.target = null;
}
Watcher.prototype = {
    update: function () {
        this.get();
        this.node.nodeValue = this.value;
    },
    get: function () {
        this.value = this.vm[this.name];
    }
}

Vue实例对象

function Vue(options) {
    this.data = options.data;
    var data = this.data;
    observe(data, this);
    var id = options.el;
    var dom = nodeToFragment(document.getElementById(id), this);
    document.getElementById(id).appendChild(dom)
}

创建Vue实例对象

var vm = new Vue({
    el: 'app',
    data: {
        text: 'hello world',
        demo: 'ppp'
    }
});

html代码

<div id="app">
        <input type="text" v-model="text">
        {{text}}
    </div>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.