美文网首页
vue3自定义指令

vue3自定义指令

作者: 焚心123 | 来源:发表于2022-10-19 14:21 被阅读0次
自定义指令的钩子
  • 局部指令
    • 在<script setup>
<script setup>
const vFocus = {
  mounted: (el) => el.focus(),
};
</script>
<template>
  <input v-focus />
</template>
  • 在export default中
<script>
export default {
  props: {
    tt: Number,
  },
  directives: {
    focus: {
      mounted(el) {
        el.focus();
      },
    },
  },
  setup(props) {
    console.log(props);

    return {};
  },
};
</script>
<template>
  <input v-focus />
</template>
  • 全局指令
const app = createApp({})

// 使 v-focus 在所有组件中都可用
app.directive('focus', {
  /* ... */
})

相关文章

网友评论

      本文标题:vue3自定义指令

      本文链接:https://www.haomeiwen.com/subject/fbvezrtx.html