首页 > 其他分享 >vue2.0是如何监听双向绑定的?

vue2.0是如何监听双向绑定的?

时间:2024-03-22 16:58:58浏览次数:24  
标签:绑定 const res Object userInfo defineProperty obj 监听 vue2.0

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
            .contanier{
                width: 300px;
                height: 300px;
                background-color: aliceblue;
                border-radius: 10px;
            }
            .contanier-first,.contanier-second{
                text-align: left;
                font-size: 28px;
                padding: 30px 100px;
            }
        </style>
    </head>
    <body>
        <div class="contanier">
            <div class="contanier-first">
                <span>姓:</span>
                <span class="first"></span>
            </div>
            <div class="contanier-second">
                <span>名:</span>
                <span class="second"></span>
            </div>
        </div>
        <input type="text" oninput="userInfo.name=this.value"/>
    </body>
    <script src="./js/new.js"></script>
    <script src="./js/index.js"></script>
</html>
function observe(obj){
    for(const key in obj){
        let internalValue = obj[key];
        let funcs = [];
        Object.defineProperty(obj,key,{
            get(){
                if(window.__func && !funcs.includes(window.__func)){
                    funcs.push(window.__func);
                }
                return internalValue;
            },
            set(val){
                internalValue = val;
                for(var i=0;i<funcs.length;i++){
                    funcs[i]();
                }
            }
        })
    }
}

function autorun(fn){
    window.__func = fn;
    fn();
    window.__func = null;
}
const userInfo = {
    name: '张三丰'
}

observe(userInfo)

const firstPage = function () {
    const res = document.querySelector(".first");
    res.textContent = userInfo.name.charAt(0);
}

const secondPage = function () {
    const res = document.querySelector(".second");
    res.textContent = userInfo.name.slice(1);
}

autorun(firstPage);
autorun(secondPage);

userInfo.name = "陈独秀";

Object.defineProperty的缺点

如果data的层级过深,就会一次性递归到底,计算量很大。
Object.defineProperty无法监听新增、删除属性。 (vue中需使用$set();方法新增双向绑定属性)
Object.defineProperty不具备监听原生数组。

标签:绑定,const,res,Object,userInfo,defineProperty,obj,监听,vue2.0
From: https://www.cnblogs.com/Jansens520/p/18089851

相关文章

  • 动态控件之控件的数据绑定
    一、示例需求说明以进度条ProgressBar控件为例,C#动态生成ProgressBar实例_progressBar,并指定_progressBar.IsVisible=IsBusy,当iIsBusy发生变化时,需要产生相应的效果。 二、方式一定义属性:bool_isBusy=false;publicboolIsBusy{get=>_isBusy;set{......
  • ruoyi-nbcio-plus基于vue3的flowable增加开始节点的表单绑定修改
    更多ruoyi-nbcio功能请看演示系统gitee源代码地址前后端代码:https://gitee.com/nbacheng/ruoyi-nbcio演示地址:RuoYi-Nbcio后台管理系统http://122.227.135.243:9666/更多nbcio-boot功能请看演示系统 gitee源代码地址后端代码:https://gitee.com/nbacheng/nbcio-boot......
  • 前端基础之原生js事件绑定案例
    原生js事件绑定开关灯案例<script><divid="d1"class="c1bg_greenbg_red"></div><buttonid="d2">变色</button><script>letbtnEle=document.getElementById('d2')letdivEle=docum......
  • java细节篇之动态绑定机制
    大家好,我是教授.F动态绑定机制,在对象上体现。一个对象有编译类型和运行类型,运行类型看=号的右边,编译类型看=号的左边。例如:publicTest{publicstaticvoidmain(String[]args){Animalanimal=newDog();}}classAniaml{}classDogextends......
  • WPF MVVM模式ListBox下ContextMenu绑定Command的方法以及将所选的Item的数据传回去
    需求:ListBoxItem上右键,将数据传参。疑问:ContextMenu不继承DataContext,导致直接用RelativeSource找根的方式也绑定不到。解决方法:在ListBox.ContextMenu里写菜单,就可以直接绑定到ViewModel层的命令了,参数先用RelativeSource找到ContextMenu,再绑定PlacementTarget.SelectedItem。......
  • 前端学习-vue学习007-计算属性+Class 与 Style 绑定
    官方教程链接Class与Style绑定Vue专门为class和style的v-bind用法提供了特殊的功能增强<span:class="{done:item.done}">{{item.text}}</span>如果item.done是true,以上代码实际为<span:class="done">{{item.text}}</span>如果item.done是false,......
  • Vue的MVVM模式与双向绑定原理
    v-model 是Vue.js框架中用于实现双向数据绑定的指令。它充分体现了MVVM(Model-View-ViewModel)模式中的双向数据绑定特性。下面我们将详细解释 v-model 如何体现MVVM和双向绑定:1.MVVM模式MVVM模式是一种软件架构设计模式,它将应用程序分为三个部分:Model(模型):代表应用程......
  • kanzi快速调试绑定
    kanzi绑定里有很多复杂表达式,难以看出输入值和结果的关系,因为绑定源可能路径复杂,也不易去修改调试。尝试做一个插件,简化调试。1.插件1.1设计思路插件提供属性InputBindText,用于解析绑定内容,提取输入的属性,转为本节点属性。生成新的绑定内容,用于手动绑定。插件相关请......
  • Golang案例开发之gopacket监听网卡抓包(2)
    文章目录前言二、实践监听网卡抓包1.代码2.知识点OpenLive方法SetBPFFilter断言总结前言本节实战,监听指定网卡,进行网络抓包,根据分层,解析不同分层包的内容。二、实践监听网卡抓包1.代码代码如下(示例):packagemainimport( "fmt" "log" "......
  • 在winform中如何实现双向数据绑定?
    什么是双向数据绑定?双向数据绑定是一种允许我们创建持久连接的技术,使模型数据和用户界面(UI)之间的交互能够自动同步。这意味着当模型数据发生变化时,UI会自动更新,反之亦然。这种双向数据绑定极大地简化了UI和模型数据之间的同步,使开发者可以更专注于业务逻辑,而不是手动处理UI和数......