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

更新日期: 2019-09-07阅读: 2.3k标签: 双向绑定

首先我们来看一些,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])
		}
	}
}

链接: https://www.fly63.com/article/detial/5192

原生js实现数据双向绑定的三种方式总汇

前端的视图层和数据层有时需要实现双向绑定(two-way-binding),例如mvvm框架,数据驱动视图,视图状态机等,研究了几个目前主流的数据双向绑定框架,总结了下。目前实现数据双向绑定主要有以下三种。

Vue双向绑定的实现原理

vue.js 采用数据劫持的方式,结合发布者-订阅者模式,通过Object.defineProperty()来劫持各个属性的setter,getter以监听属性的变动,在数据变动时发布消息给订阅者,触发相应的监听回调.

vue双向数据绑定失效_解决vue添加新属性实现双向绑定

vue框架目前在前端开发使用比较广了,但是又很多同学发现vue创建对象之后添加新的属性实现不了双向绑定,下面就简单介绍如何解决vue双向绑定出现失效的问题

vue2 实现 div contenteditable=true 类似于 v-model双向数据绑定的效果

用到 contenteditable=true的 div ,而在这个 div 上是使用 v-model 是没有效果的,这里的双向数据绑定该如何实现?解决思路:一自定义指令,二使用组件。

自定义 Vue 中的 v-model 双向绑定

v-model 双向绑定实际上就是通过子组件中的 $emit 方法派发 input 事件,父组件监听 input 事件中传递的 value 值,并存储在父组件 data 中;然后父组件再通过 prop 的形式传递给子组件 value 值,再子组件中绑定 input 的 value 属性即可。

vue双向绑定原理分析

当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理。vue.js 则是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter

React input表单双向数据绑定仿Vue v-model实现

用过Vue的同学都知道,Vue里<input> 、 <textarea> 及 <select>等表单元素可以通过v-model指令实现双向数据绑定,也就是说,当用户通过交互改变表单的值时,表单绑定的数据也会同步响应,这一点也是Vue对开发人员非常友好的点之一。

js 超浓缩 双向绑定

绑定确实是个有趣的话题。现在我的绑定器有了不少的功能1. 附着在Object对象上,一切以对象为中心2. 与页面元素进行双向绑定

Vue - 自定义组件双向绑定

无论在任何的语言或框架中,我们都提倡代码的复用性。对于Vue来说也是如此,相同的代码逻辑会被封装成组件,除了复用之外,更重要的是统一管理提高开发效率。我真就接手过一个项目,多个页面都会用到的列表

VUE:computed 和 v-model 配合使用,双向绑定失效

今天在实现一个表单赋值并修改的功能时,由于其中数值直观显示不美观,所以使用了 computed 计算属性,同时使用 v-model 的双向绑定,来处理显示。但却发现 v-model 的双向绑定失效了,控制台警告信息如下:

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!