<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-my="msg" />
</div>
<script>
Vue.directive("my", {
// 获取dom对象绑定input事件
inserted: function (el, binding, vnode) {
if (!binding.value) {
return console.error("v-my没有被赋值");
}
// 默认值为msg
el.value = binding.value;
el.oninput = function (e) {
console.log(vnode.context.msg);
vnode.context.msg = e.target.value;
};
},
// 虚拟dom节点更新时调用
update: (el, binding, vnode) => {
el.value = binding.value;
},
});
var vm = new Vue({
el: "#app",
data: {
msg: "bbb",
},
});
</script>
</body>
</html>
v-model指令简易实现
最新推荐文章于 2024-07-19 11:46:42 发布