Vue双向数据绑定原理

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <input placeholder="请输入用户名"  id ="username" type="text">
    显示值:<p id="uName"></p>  
</head>
<body>
    <script>
let obj ={}
debugger
Object.defineProperty(obj,'username',{
    // 取值
    get:function(){
        console.log('取值');
    },
    set:function(val){
        console.log('设置值');
        document.getElementById("uName").innerText = val
    }
})
document.getElementById("username").addEventListener("keyup",function(){
      //event
      obj.username = event.target.value
})

    </script>
</body>
posted @ 2022-04-02 15:14  ajaXJson  阅读(14)  评论(0编辑  收藏  举报