webfansplz / vuejs-challenges

Collection of Vue.js challenges
https://vuejs-challenges.netlify.app/
MIT License
2.7k stars 188 forks source link

19 - 切换焦点指令 #2675

Open c11ee opened 4 months ago

c11ee commented 4 months ago
<script setup lang="ts">
import { ref } from 'vue'

const state = ref(false)

type FnType = (el: HTMLInputElement, { value }: { value: boolean }) => void
const handleFocus: FnType = (el, { value }) => el[value ? 'focus' : 'blur']()

const vFocus = {
  mounted: handleFocus,
  updated: handleFocus
}

setInterval(() => {
  state.value = !state.value
}, 2000)
</script>
<template>
  <input v-focus="state" type="text" />
</template>