简单实现一个VUE的双向绑定

北城余情 提交于 2019-11-29 07:59:41

首先我们来看一些,vue的基本使用方法

new Vue({
	el:'#app',
	data:{
		price:27,
		info:{
			title:'猪肉的价格'
		},
		name:23
	},
	beforeCreate() {
	// console.log(this.info)
	},
	created(){
	// console.log(this.info)		
	},
	beforeMount () {
	// console.log(document.querySelector('#home'))
	},
	mounted(){
	// console.log(document.querySelector('#home'))
	},
	render(createElement) {
	return createElement('div',{
		attrs:{
			title:this.info.title,
			id:'home'
		}
	},[
		createElement('span',{},this.price)
	])
	}
	})

然后我们根据使用方法,来设计一个类和一些基础的声明周期

	class Vue {
	constructor(options) {
		this.$el = document.querySelector(options.el) // 获取根元素
		this.beforeCreate = options.beforeCreate // 生命周期 beforeCreate
		if(this.beforeCreate)this.beforeCreate()
	    this._data = options.data || options.data() // 获取初始数据
		this._render = options.render // 获取渲染函数
		new Observer(this._data) // 进行深度代理
		for ( let key in this._data){  // 代理data 元素,把元素代理到this实例上
			proxy(this,this._data,key)
		}
		this.created = options.created // 生命周期 created
		if(this.created)this.created()
		this.beforeMount = options.beforeMount // 生命周期 beforeMount
		if(this.beforeMount)this.beforeMount()
		new Watch(()=>{
			this._update()  // 渲染页面
		})
		this.mounted = options.mounted // 生命周期 mounted
		if(this.mounted)this.mounted()
	}
	_update () { // 渲染函数
		const node = this._render(this._createElement) // 执行渲染函数
		replaceChild(node,this.$el)
		this.$el = node
	}
	_createElement (targetName,data,chilren) {
		const tag = document.createElement(targetName)
		const {attrs = {}} = data
		for ( let attr in attrs){
			tag.setAttribute(attr,attrs[attr])
		}
		if(Object.prototype.toString.call(chilren) !== '[object Array]'){
			let child = document.createTextNode(chilren) // 创建一个文本节点
			tag.appendChild(child)
		}else {
			chilren.forEach(child => {
				tag.appendChild(child)
			})
		}
		return tag
	}
}

function replaceChild (newNode,oldNode) {
	return oldNode.parentElement.replaceChild(newNode,oldNode)
}
function proxy (target,data,key) { // 代理函数,代理最外层data
	Object.defineProperty(target,key,{
		get () {
			return data[key]
		},
		set (newValue) {
			data[key] = newValue
		}
	})
}
function defineProperty (target,key,value) { // 深度监听
	const dep = new Dep()
	Object.defineProperty(target,key,{
		get () {
			if(Dep.targets.length>0){  // 为了排除后面来的
				dep.addDepend()
			}
			return value
		},
		set (newValue) {
			value = newValue
			dep.notify()
		}
	})
}
class Dep { // 发布订阅者
	constructor(arg) {
	    this.subs = []
	}
	addSub(obj){ // 添加,
		if(this.subs.indexOf(obj) === -1) { // 如不存在该对象,则添加该对象
			this.subs.push(obj)
		}
	}
	notify(){ // 发布
		for(let i = 0;i<this.subs.length;i++){
			this.subs[i].update()
		}
	}
	addDepend () {
		Dep.targets[Dep.targets.length-1].addDep(this)
		
	}
}
Dep.targets = [] // 添加
function pushTarget (instance) { // 入栈
	Dep.targets.push(instance)
}
function popTarget () { // 出栈
	return Dep.targets.pop()
}
class Watch { // 事件监听类
	constructor(getter){
		this.getter = getter
		this.get()
	}
	get () {
		pushTarget(this)
		let value = this.getter()
		popTarget()
		return value
	}
	addDep (dep) {
		dep.addSub(this)
	}
	update () {
		this.getter()
	}
}
class Observer {
	constructor(obj) {
	    this.walk(obj)
	}
	walk (obj) {
		for(let key in obj){
			if(typeof obj[key] === 'object'){
				this.walk(obj[key])
			}
			defineProperty (obj,key,obj[key])
		}
	}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!