vuejs-challenges icon indicating copy to clipboard operation
vuejs-challenges copied to clipboard

19 - 切换焦点指令

Open PoliWen opened this issue 2 years ago • 0 comments

<script setup lang='ts'>
import { ref } from "vue"

const state = ref(false)

/**
 * Implement the custom directive
 * Make sure the input element focuses/blurs when the 'state' is toggled
 *
*/

const VFocus = {
  mounted(el,_binding) {
    _binding.value ? el.focus() : el.blur()
  },
  updated(el,_binding) {
    _binding.value ? el.focus() : el.blur()
  }

}

setInterval(() => {
  state.value = !state.value
}, 2000)

</script>

<template>
  <input v-focus="state" type="text">
</template>

PoliWen avatar Sep 06 '22 15:09 PoliWen