Vue3封装自定义指令+h()

官方install介绍
在这里插入图片描述directive/myDir/index.js
定义指令

import { h, render, ref } from "vue";
const vMyDir = {
  mounted(el, binding) {
    renderElement(el, binding);
  },
};
// inserted是Vue2的生命周期钩子,所以在Vue3项目中要使用mounted
const renderElement = (el, binding) => {
  // el就是指令所绑定的元素,binding.value是传进来的指令值
  console.log(el, binding.value);

  // el.innerHTML获取元素的文本内容
  console.log(el.innerHTML);

  // el.style.color = binding.value.color;
  // el.style.backgroundColor = "green";
  // const foo = { fontSize: "30px" };

  const foo = "some-name";
  let isActive = ref(true);

  const style1 = {
    color: "pink",
  };

  const style2 = {
    color: "pink",
    backgroundColor: "green",
  };
  let myStyle;
  if (isActive.value) {
    myStyle = style2;
  } else myStyle = style1;
  const vnode = h(
    "div",
    { class: [foo], style: myStyle },
    //   { class: [foo], style: { color: "pink" } },
    //   {
    //     class: { foo: isActive.value },
    //     style: { color: "pink", backgroundColor: "green" },
    //   },
    "hello"
  );

  render(vnode, el);
};
export default vMyDir;


index.vue 使用指令

<div class="vvv" v-my-dir="{ color: 'red' }">999</div>




directives/index.js

import myDir from "./myDir";
import closeTo from "./closeTo";
// 指令对象
const directives = {
  myDir,
  closeTo,
};

export default {
  install(app) {
    console.log("directives", directives);

    console.log("Object.keys(directives)", Object.keys(directives));

    Object.keys(directives).forEach((key) => {
      console.log("key, directives[key]", key, directives[key]);

      app.directive(key, directives[key]);
    });
  },
};
// Object.keys(directives).forEach((key) => { ... }):这是一个遍历指令对象 directives 的循环。
// Object.keys() 方法返回一个包含指令对象中所有属性名称的数组。
// app.directive(key, directives[key]):使用Vue的directive方法注册指令。
// key 是指令名称,directives[key] 是对应的指令对象。通过这个循环,将所有的指令都注册到应用程序中。

在这里插入图片描述



main.js
import { createApp } from "vue";
import { createPinia } from "pinia";
import "virtual:uno.css";
import App from "./App.vue";
import router from "./router";
import directives from "./directives";
const app = createApp(App);
app.use(directives);

app.use(createPinia());
app.use(router);

app.mount("#app");


在其他dom上绑定元素

在这里插入图片描述directive/myDir/index.js

import { h, render, ref } from "vue";
const vMyDir = {
  mounted(el, binding) {
    renderElement(el, binding);
  },
};

const renderElement = (el, binding) => {
  // binding.value 是指绑定到指令的值,而不是指令所在元素的引用。
  // 如果你希望访问 father 变量所引用的元素,
  // 你应该使用 binding.instance 来获取指令所在的组件实例,
  // 然后通过 binding.instance.$refs 来访问 father 引用的元素。
  const fatherElement = binding.instance.$refs.father;
  console.log(fatherElement); // 打印出绑定到 "father" 的元素
  console.log("binding.instance", binding.instance);
  //   请注意,在 Vue 3 中,
  //   除了 binding.value 和 binding.instance,binding.arg 和 binding.modifiers 字段也是可用的,
  //   以提供更多配置和参数信息

  const foo = "some-name";
  let isActive = ref(true);

  const style1 = {
    color: "pink",
  };

  const style2 = {
    color: "pink",
    backgroundColor: "green",
  };
  let myStyle;
  if (isActive.value) {
    myStyle = style2;
  } else myStyle = style1;
  const vnode = h("div", { class: [foo], style: myStyle }, "hello");

  render(vnode, fatherElement); //!!!!!!!!!!!!!
  //   render(vnode, el);
};
export default vMyDir;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封装自定义指令,你可以按照以下步骤进行操作: 1. 创建一个新的 Vue 指令。在 Vue 3 ,可以使用 `app.directive` 方法来创建指令。 ```javascript app.directive('myDirective', { // 指令选项 }); ``` 2. 在指令选项,你需要定义以下几个钩子函数(可选): - `beforeMount`:在指令绑定元素挂载到DOM之前调用。 - `mounted`:在指令绑定元素挂载到DOM后调用。 - `beforeUpdate`:在指令所在组件更新之前调用。 - `updated`:在指令所在组件更新之后调用。 - `beforeUnmount`:在指令所在组件卸载之前调用。 - `unmounted`:在指令所在组件卸载后调用。 这些钩子函数允许你在指令的生命周期执行相应的操作。 3. 在钩子函数,你可以使用 `el` 参数获取到指令所绑定的 DOM 元素。通过操作 DOM 元素,你可以实现指令的功能。 下面是一个示例,演示了如何封装一个简单的自定义指令来实现点击元素时改变背景颜色的效果: ```javascript app.directive('changeBackground', { beforeMount(el) { el.style.backgroundColor = 'yellow'; }, mounted(el) { el.addEventListener('click', this.onClick); }, beforeUnmount(el) { el.removeEventListener('click', this.onClick); }, onClick(event) { event.target.style.backgroundColor = 'red'; } }); ``` 在上面的示例,指令名称为 `changeBackground`,在 `beforeMount` 钩子函数设置初始背景颜色为黄色,在 `mounted` 钩子函数添加点击事件监听器,点击元素时调用 `onClick` 方法来改变背景颜色为红色,在 `beforeUnmount` 钩子函数移除点击事件监听器。 你可以根据自己的需求,在钩子函数实现自定义指令的具体功能。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值