原生js开发vue的双向数据绑定
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id='app'>
<input style="width: 200px;height: 38px;outline: none;display: inline-block;" type='text' v-model='text' />{{text}}
<input style="width: 200px;height: 38px;outline: none;display: inline-block;" type='text' v-model='name' />{{name}}
<div style="width: 200px;height: 40px;background: red;" v-if='show'></div>
</div>
<script>
function Vue(options){
this.data=options.data;
var id=options.el;
var data=this.data;
this.observe(data,this)
var dom=this.tofagerment(document.getElementById(id),this);
var pardom=document.getElementById(id).appendChild(dom)
}
Vue.prototype.tofagerment=function(node,vm){
var flag=document.createDocumentFragment();
var child="";
while(child = node.firstChild){
this.compile(child,vm)
flag.append(child);
}
return flag
}
Vue.prototype.compile=function(child,vm){
var name=""
if(child.nodeType==1){
if(child.hasAttribute('v-model')){
name=child.getAttribute('v-model');
child.addEventListener('input',function(e){
var e=e||window.event;
var target=e.target||e.srcElement;
vm[name]=target.value;
})
child.value=vm[name];
child.removeAttribute('v-model')
}
}else if(child.nodeType==3){
var reg=/\{\{(.*)\}\}/;
if(reg.test(child.nodeValue)){
var regname=reg.exec(child.nodeValue)[1];
regname=regname.trim();
child.nodeValue=vm[regname]
}
new Watcher(vm,child,regname)
};
}
//订阅
Vue.prototype.observe=function(obj,vm){
Object.keys(obj).forEach(function(key){
console.log(obj[key])
vm.defineReactive(vm,key,obj[key])
})
}
// 消费
Vue.prototype.defineReactive=function(obj, key , value){
var dep=new Dep()
Object.defineProperty(obj,key,{
get:function(){
console.log('获取值');
if(Dep.target){
console.log(32423423)
dep.addSub(Dep.target)
}
return value
},
set:function(newValue){
console.log('设置值'+newValue);
if(newValue==value)return ;
value=newValue;
dep.notify()
}
})
}
//观察者模式
function Watcher(vm,node,name){
Dep.target=this
this.vm=vm;
this.node=node;
this.name=name;
this.update();
Dep.target=null
}
Watcher.prototype.get=function(){
this.value=this.vm[this.name]
}
Watcher.prototype.update=function(){
this.get()
this.node.nodeValue=this.value
}
//发布者
function Dep(){
this.subs=[];//订阅客户
}
Dep.prototype.addSub=function(sub){
this.subs.push(sub)
}
Dep.prototype.notify=function(sub){
this.subs.forEach(function(item){
item.update()
})
}
var myvue=new Vue({
el:"app",
data:{
text:'1312323123',
name:"eqwewqeqeq",
show:false,
}
})
</script>
</body>
</html>
原生js开发vue的双向数据绑定的更多相关文章
- Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令
自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...
- 原生js开发,无依赖、轻量级的现代浏览器图片懒加载插件,适合在移动端开发使用
优势 1.原生js开发,不依赖任何框架或库 2.支持将各种宽高不一致的图片,自动剪切成默认图片的宽高 比如说你的默认图片是一张正方形的图片,则各种宽度高度不一样的图片,自动剪切成正方形. 完美解决移动 ...
- 面试题:你能写一个Vue的双向数据绑定吗?
在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...
- vue实现双向数据绑定的原理
vue实现双向数据绑定的原理就是利用了 Object.defineProperty() 这个方法重新定义了对象获取属性值(get)和设置属性值(set)的操作来实现的. 在MDN上对该方法的说明是:O ...
- 浅析vue的双向数据绑定
vue 的双向数据绑定是基于es5的 object对象的defineProperty属性,重写data的set和get函数来实现的.1.defineProperty 数据展示 Object.defin ...
- vue的双向数据绑定实现原理
在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...
- 原生js实现数据的双向绑定
原生js实现数据的双向绑定 需要了解的属性是原色js的Object.definePrototype(obj,pop,descript); obj:被构造的对象 pop:被构造的对象的属性,创建对象或修 ...
- 一个关于原生 js 开发一款插件的前端教程
教程链接: http://www.codeasily.net/course/plugin_course/ 写的不是很好,前面比较松后面比较急,请大家见谅,本人也没多少年前端经验,拿以前写过的教程网站, ...
- 原生js实现 vue的数据双向绑定
原生js实现一个简单的vue的数据双向绑定 vue是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时 ...
随机推荐
- Java design patterna
Java中的设计模式 设计模式是解决特定问题/任务的充分证明的解决方案. 现在,一个问题会在你脑海中产生什么样的具体问题?让我举个例子来解释一下. 给出的问题:假设您要创建一个只应创建单个实例(或对象 ...
- [Python学习笔记] turtle库的基本使用
turtle库常用函数 引入turtle模块 import turtle turtle的绘图窗体 #setup()设置窗口大小及位置#setup()可省略turtle.setup(width,heig ...
- kafka消息机制
https://www.infoq.cn/article/kafka-analysis-part-1 https://www.infoq.cn/article/kafka-analysis-part- ...
- RABBITMQ too many heartbeats missed
执行rabbitmqctl status | grep -A 4 file_descriptors 显示socket_used 达到 socket_limited 的值 增加socket_limi ...
- Mac OS X L2TP Client Setup
原文链接:http://www.softether.org/4-docs/2-howto/9.L2TPIPsec_Setup_Guide_for_SoftEther_VPN_Server/5.Mac_ ...
- vim-go 安装
vim-go 安装 https://studygolang.com/articles/3229
- java中super关键字的作用
1.super关键字可以在子类的构造方法中显示地调用父类的构造方法,super()必须为子类构造函数中的第一行. 2.super可以用来访问父类的成员方法或变量,当子类成员变量或方法与父类有相同的名字 ...
- 如何使用Visual Studio Code开发Django项目
如何获得 Visual Studio Code 访问 http://code.visualstudio.com 下载并安装. 前提条件 安装Python 2.7 及 Python 3.5,Window ...
- (转)PHP线程安全与非线程安全的区别:如何选择用哪一个?
PHP线程安全与非线程安全的区别:如何选择用哪一个? 很多时候,我们在做PHP环境配置的时候,很多人都是直接去乱下载PHP版本的,但是他不清楚:从2000年10月20日发布的第一个Windows版的P ...
- var entsMapLocation = {……}函数
var entsMapLocation = { global: { $popupCityBox: $(".ents-map-location-popup-box"), isPosi ...