原生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,在数据变动时 ...
随机推荐
- C语言题库----指针
1.如果f是一个函数,请说明 f() 和f的意思. f是函数的地址,f()是函数 2.怎样理解数组的下标运算? 先偏移,后取址. 3.int *p,*q; int a[]={10,20,30,40}; ...
- Centos7快速安装docker
偶然间发现,docker的安装好容易啊 系统环境:centos7.3 yum源: docker:https://mirrors.aliyun.com/docker-ce/linux/centos/do ...
- redis scan删除key的方法封装
/** * @desc 迭代式的删除redis key * 用法: * $redis = BaseService::S()->getRedisConfig(\Yii::$app->redi ...
- Zuul网关总结
Zuul是Netflix开源的网关服务(gateway service)(https://github.com/Netflix/zuul),提供动态路由.监控.弹性.安全性等功能.最近在公司的项目中用 ...
- pyhanlp:hanlp的python接口
HanLP的Python接口,支持自动下载与升级HanLP,兼容py2.py3. 安装 pip install pyhanlp 使用命令hanlp来验证安装,如因网络等原因自动安装失败,可参考手动配置 ...
- flutter mac 下安装
- Ubuntu 16.04 安装Go 1.9.2
系统环境 Ubuntu: 16.04 Go: 1.9.2 安装步骤 $ curl -O https://storage.googleapis.com/golang/go1.9.linux-amd64. ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
- 贝叶斯、朴素贝叶斯及调用spark官网 mllib NavieBayes示例
贝叶斯法则 机器学习的任务:在给定训练数据A时,确定假设空间B中的最佳假设. 最佳假设:一种方法是把它定义为在给定数据A以及B中不同假设的先验概率的有关知识下的最可能假设 贝叶斯理论提供了 ...
- 基础总结(03)-- css有趣的特性
1.currentColor:可用于background/border-color/渐变/box-shadow/svg填充色,颜色继承自color. 待补充…