程序员鸡皮

文章 分类 评论
95 3 22

站点介绍

一名PHP全栈程序员的日常......

Vue3自定义指令基本使用(1)

abzzp 2024-08-31 1158 0条评论 前端 vue

首页 / 正文
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......

发布于2024-07-04

方式一

定义ref绑定到input中, 调用focus

<template> 
  <div class="app">
    <input type="text" ref="inputRef">
  </div>
</template>

<script setup>
// 1.方式一: 定义ref绑定到input中, 调用focus
import useInput from "./hooks/useInput";
const { inputRef } = useInput();
</script>

<style lang="less" scoped>

</style>

其中,src的/hooks/useInput.js文件

import { ref,onMounted } from 'vue';

export default function useInput(){
  const inputRef = ref()
 
  onMounted(()=>{
    inputRef.value?.focus()
  })

  return { inputRef }
}

方式二:自定义指令(局部指令)

Composition-API

<template>
  <div class="app">
    <input type="text" v-focus>
  </div>
</template>

<script setup>
// 2.方式二:自定义指令(局部指令)
const vFocus = {
  // 生命周期的函数(自定义指令)
  mounted(el){
    console.log("v-focus应用的元素被挂载了")
    el?.focus();
  }
}
</script>

<style lang="less" scoped>

</style>

Options Api写法

<template> 
  <div class="app">
    <input type="text" v-focus>
  </div>
</template>

<script>
  export default{
    directives:{
      focus:{
        // 生命周期的函数(自定义指令)
        mounted(el){
          // console.log("v-focus应用的元素被挂载了", el)
          el?.focus()
        }
      }
    }
  } 
</script>

<style lang="less" scoped>

</style>

方式三:自定义指令(全局指令)

使用app.directive()函数,在main.js文件中修改

import { createApp } from 'vue'
import App from './01_自定义指令/App.vue'

const app = createApp(App);
app.directive("focus",{
  // 生命周期的函数(自定义指令)
  mounted(el){
    console.log("v-focus应用的元素被挂载了",el)
    el.focus();
  }
})
app.mount('#app')

当然我们把所有自定义元素都放在main.js中显得太乱了,所以我们可以将它抽离出来

// main.js
import { createApp } from 'vue'
import App from './01_自定义指令/App.vue'
import useDirectives from './01_自定义指令/directives/index'
const app = createApp(App);
// app.directive("focus",{
//   // 生命周期的函数(自定义指令)
//   mounted(el){
//     console.log("v-focus应用的元素被挂载了",el)
//     el.focus();
//   }
// })

// 自定义指令方式一:
useDirectives(app)

app.mount('#app')

引入的文件

// directives/index.js

import directiveFocus from "./focus"

export default function directives(app) {
  directiveFocus(app)
}
// directives/focus.js
export default function directiveFocus(app) {
  app.directive("focus", {
    // 生命周期的函数(自定义指令)
    mounted(el) {
      // console.log("v-focus应用的元素被挂载了", el)
      el?.focus()
    }
  })
}

感谢大家观看,我们下期再见

评论(0)

最新评论

  • fintechbase

    It's not my first time to pay a visit this web page, i am visiting this website dailly and get good facts from here every day.

  • fintechbase

    Hello, i think that i saw you visited my site so i got here to go back the want?.I'm trying to find things to improve my web site!I suppose its adequate to use a few of your concepts!!

  • fintechbase

    Touche. Outstanding arguments. Keep up the great effort.

  • fintechbase

    Wow, this post is good, my younger sister is analyzing these things, therefore I am going to let know her.

  • digital banking

    There's certainly a lot to learn about this topic. I like all the points you've made.

  • 觀景碼頭

    所有文章都令人印象深刻。由衷感谢 带来的灵感。

  • 圣巴托罗梅奥

    你 百分百 分享经验。坚持!

  • 孤懸外海

    不常看到, 这么高质量的内容。谢谢。

  • abzzp

    @十二使徒岩 哪张图片?

  • 十二使徒岩

    照片令人惊艳。继续保持 心情。

日历

2026年01月

    123
45678910
11121314151617
18192021222324
25262728293031

文章目录

站点公告
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......
点击小铃铛关闭
配色方案