<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input v-model="text"><br>
<input v-model="text"><br>
<h2 v-bind="text"></h2>
<hr>
<input v-model="title"><br>
<input v-model="title"><br>
<h2 v-bind="title"></h2>
<script>
let proxy = new Proxy({},{
get(obj,key){
},
set (obj,key,val){
let models=document.querySelectorAll(`[v-model=${key}]`);
models.forEach(item=>{
item.value=val;
})
let binds=document.querySelectorAll(`[v-bind=${key}]`);
binds.forEach(item=>{
item.innerHTML=val;
})
}
})
function init(){
let inputs=document.querySelectorAll("[v-model]");
inputs.forEach(item=>{
item.addEventListener("keyup",function(event){
proxy[this.getAttribute("v-model")]=this.value;
})
})
}
init();
</script>
</body>
</html>
proxy实现vue双向绑定
最新推荐文章于 2024-11-13 16:57:55 发布