A
A
asferot2019-07-31 15:13:29
Vue.js
asferot, 2019-07-31 15:13:29

How to run keyup.esc event handler?

modal-call(:class="{active: showModal}" :keyup.esc="showForm")
:keyup.esc - I want to use it to close the window, but it doesn't work I've looked at
the previous solutions, but it doesn't work the same

data(){
        return{ showModal: false }
    },
    methods:{
        showForm(){
            this.showModal = !this.showModal
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AngKov, 2020-02-27
@AngKov

mounted () {
            const escapeHandler = (e:any) => {
                if (e.key === `Escape` && this.showModal) {
                    this.showForm()
                }
            }
            document.addEventListener(`keydown`, escapeHandler)
            this.$once(`hook:destroyed`, () => {
                document.removeEventListener(`keydown`, escapeHandler)
            })
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question