Vue3的自定义指令怎么迁移到nuxt3

一、找到Vue3中指令的源码

const DISTANCE = 100; // 距离
const ANIMATIONTIME = 500; // 500毫秒
let distance: number | null = null,
  animationtime: number | null = null;
const map = new WeakMap();
const ob = new IntersectionObserver((entries) => {
  for (const entrie of entries) {
    if (entrie.isIntersecting) {
      const animation = map.get(entrie.target);
      if (animation) {
        animation.play();
        ob.unobserve(entrie.target);
      }
    }
  }
});

function isBelowViewport(el: HTMLElement) {
  const rect = el.getBoundingClientRect();
  return rect.top - (distance || DISTANCE) > window.innerHeight;
}

export default {
  mounted(el: HTMLElement, binding: any) {
    if (binding.value) {
      // console.log(binding.value);
      distance = binding.value.px;
      animationtime = binding.value.time;
    }
    if (!isBelowViewport(el)) {
      return;
    }
    const animation = el.animate(
      [
        {
          opacity: 0,
          transform: `translateY(${distance || DISTANCE}px)`,
        },
        {
          opacity: 1,
          transform: `translateY(0px)`,
        },
      ],
      {
        duration: animationtime || ANIMATIONTIME,
        fill: "forwards",
        easing: "ease-in-out",
      }
    );
    animation.pause();
    map.set(el, animation);
    ob.observe(el);
  },

  unmounted(el:HTMLElement) {
    ob.unobserve(el);
  },
};

二、源码迁移到nuxt3

存放目录为指定的的plugins文件夹,没有这个文件夹自己新建
然后新建一个文件xx.ts用于存放修改之后的指令代码, 我这里使用toTop.ts
所以路径是 plugins/toTop.ts 下面是文件内容

const DISTANCE = 100; // 距离
const ANIMATIONTIME = 500; // 500毫秒
let distance: number | null = null,
  animationtime: number | null = null;
const map = new WeakMap();
const ob =
  process.client &&
  new IntersectionObserver((entries) => {
    for (const entrie of entries) {
      if (entrie.isIntersecting) {
        const animation = map.get(entrie.target);
        if (animation) {
          animation.play();
          ob.unobserve(entrie.target);
        }
      }
    }
  });

function isBelowViewport(el: HTMLElement) {
  const rect = el.getBoundingClientRect();
  return rect.top - (distance || DISTANCE) > window.innerHeight;
}

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.directive("top", {
    mounted(el, binding) {
      if (binding.value) {
        // console.log(binding.value);
        distance = binding.value.px;
        animationtime = binding.value.time;
      }
      if (!isBelowViewport(el)) {
        return;
      }
      const animation = el.animate(
        [
          {
            opacity: 0,
            transform: `translateY(${distance || DISTANCE}px)`,
          },
          {
            opacity: 1,
            transform: `translateY(0px)`,
          },
        ],
        {
          duration: animationtime || ANIMATIONTIME,
          fill: "forwards",
          easing: "ease-in-out",
        }
      );
      animation.pause();
      map.set(el, animation);
      ob.observe(el);
    },
    unmounted(el: HTMLElement) {
      ob.unobserve(el);
    },
    getSSRProps(binding, vnode) {
      // 你可以在这里提供SSR特定的props
      return {};
    },
  });
});

三、注册指令

在 nuxt.config.ts 文件中


export default defineNuxtConfig({
 // ...其他配置
  plugins: ["/plugins/toTop.ts"],
 // ...其他配置
});

参考的官方文档

地址

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中的自定义指令封装是一种扩展Vue的能力,允许开发者在DOM元素上添加自定义行为。下面是Vue 3中自定义指令封装的步骤: 1. 创建自定义指令:使用`app.directive`方法来创建自定义指令。该方法接受两个参数,第一个参数是指令名称,第二个参数是一个对象,包含了指令的各种钩子函数和配置选项。 2. 钩子函数:自定义指令可以通过钩子函数来定义其行为。常用的钩子函数有: - `beforeMount`:在指令绑定的元素挂载到DOM之前调用。 - `mounted`:在指令绑定的元素挂载到DOM之后调用。 - `beforeUpdate`:在指令所在组件更新之前调用。 - `updated`:在指令所在组件更新之后调用。 - `beforeUnmount`:在指令所在组件卸载之前调用。 - `unmounted`:在指令所在组件卸载之后调用。 3. 配置选项:除了钩子函数外,还可以通过配置选项来定义定义指令的行为。常用的配置选项有: - `bind`:在指令绑定到元素时立即调用,只调用一次。 - `update`:在指令所在组件的VNode更新时调用,可能会调用多次。 - `unbind`:在指令从元素上解绑时调用,只调用一次。 下面是一个示例,演示了如何在Vue 3中封装一个自定义指令: ```javascript // 创建Vue实例 const app = Vue.createApp({}); // 创建自定义指令 app.directive('my-directive', { beforeMount(el, binding, vnode) { // 指令绑定到元素之前的操作 }, mounted(el, binding, vnode) { // 指令绑定到元素之后的操作 }, beforeUpdate(el, binding, vnode) { // 指令所在组件更新之前的操作 }, updated(el, binding, vnode) { // 指令所在组件更新之后的操作 }, beforeUnmount(el, binding, vnode) { // 指令所在组件卸载之前的操作 }, unmounted(el, binding, vnode) { // 指令所在组件卸载之后的操作 } }); // 将Vue实例挂载到DOM元素上 app.mount('#app'); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值