1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <script> function Vue(options){ this.$options = options; this.$data = options.data() observer(this.$data) } Vue.prototype.$mount = function(sel){ this.updata = function(){ console.log('undata') const vnode = this.$options.render.call(this)
if(!this.isMounted){ const parent = document.querySelector(sel) this.patch(parent,vnode) this.isMounted = true parent.appendChild(child) if(this.$options.mounted){ this.$options.mounted.call(this) } }else{ this.patch(this._vnode,vnode) } this._vnode = vnode } this.updata() } Vue.prototype.createElement = function(tag,props,children){ return {tag,props,children} } Vue.prototype.patch= function(n1,n2){
} function observer(obj){ Object.keys(obj).forEach(key=>{defineReactive(obj,key,obj[key])}) }
function defineReactive(obj,key,val){ Object.defineProperty(obj,key,{ get(){ console.log('get',key) return val }, set(newVal){ if(newVal !== val){ val = newVal kvue.updata() } } }) } </script> <script> const kvue = new Vue({ data(){ return { title:'标题' } }, mounted(){ setTimeout(()=>{ this.$data.title = '标题变了' },1000) }, render(){ return h('h3',null,this.$data.title) } }) kvue.$mount('#app')
</script>
|