Vue3自定义全局指令

指令封装代码:

import type { App } from "vue";

const content ={
    mounted(el : any, binding : any) {
        console.dir(binding.value);
        el.remove();
    }
};

const operate = {
    mounted(el : any, binding : any) {
        console.dir(binding.value);
        el.remove();
    }
};

const directives : any = {
    content,
    operate
}

/*
*  指令的完整生命周期
  // 在绑定元素的 attribute 前
  // 或事件监听器应用前调用
  created(el, binding, vnode, prevVnode) {
    // 下面会介绍各个参数的细节
  },
  // 在元素被插入到 DOM 前调用
  beforeMount(el, binding, vnode, prevVnode) {},
  // 在绑定元素的父组件
  // 及他自己的所有子节点都挂载完成后调用
  mounted(el, binding, vnode, prevVnode) {},
  // 绑定元素的父组件更新前调用
  beforeUpdate(el, binding, vnode, prevVnode) {},
  // 在绑定元素的父组件
  // 及他自己的所有子节点都更新后调用
  updated(el, binding, vnode, prevVnode) {},
  // 绑定元素的父组件卸载前调用
  beforeUnmount(el, binding, vnode, prevVnode) {},
  // 绑定元素的父组件卸载后调用
  unmounted(el, binding, vnode, prevVnode) {}
* */

export function setDirective( app : App<Element>) {
    Object.keys(directives).forEach( (key: string) => {
        app.directive(key, directives[key])
    })
}

全局挂载:

import { setDirective } from '@/utils/PowerAuth'
const app = createApp(App);
setDirective(app);
提示:页面直接在元素上像使用v-if那样v-xxx即可

相关推荐

  1. Vue3定义全局指令

    2024-02-20 09:44:02       46 阅读
  2. Vue3全局组件和定义指令

    2024-02-20 09:44:02       64 阅读
  3. vue3定义指令

    2024-02-20 09:44:02       65 阅读
  4. vue3定义指令

    2024-02-20 09:44:02       70 阅读
  5. VUE3 定义指令

    2024-02-20 09:44:02       43 阅读
  6. vue3定义指令

    2024-02-20 09:44:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 09:44:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 09:44:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 09:44:02       87 阅读
  4. Python语言-面向对象

    2024-02-20 09:44:02       96 阅读

热门阅读

  1. 【C】printf和scanf函数的探索

    2024-02-20 09:44:02       41 阅读
  2. SQL Server中类似MySQL的REPLACE INTO语句

    2024-02-20 09:44:02       45 阅读
  3. SpringCloud微服务调用丢失请求头

    2024-02-20 09:44:02       39 阅读
  4. vscode 命令无法执行

    2024-02-20 09:44:02       43 阅读
  5. OpenCV:计算机视觉领域的瑞士军刀

    2024-02-20 09:44:02       52 阅读
  6. Go json Marshal & UnMarshal 的一点小 trick

    2024-02-20 09:44:02       54 阅读
  7. 【LeetCode-494】目标和(回溯&动归)

    2024-02-20 09:44:02       49 阅读
  8. 第13章 网络 Page749~755 asio核心类 ip::tcp::acceptor

    2024-02-20 09:44:02       44 阅读