Skip to content

19 - 切换焦点指令 #2957

Open
Open
@Gilfoyle-sy

Description

@Gilfoyle-sy
<script setup lang="ts">
import { ref } from 'vue'
import type { DirectiveBinding } 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: HTMLElement) => el.focus(),
  updated: (el: HTMLElement, binding: DirectiveBinding) => {
    void (binding.value ? el.focus() : el.blur())
  },
}

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Gilfoyle-sy

        Issue actions

          19 - 切换焦点指令 · Issue #2957 · webfansplz/vuejs-challenges