search

Home  >  Q&A  >  body text

Vue JS: Conditional logic inside v-on:keyup.enter

How to conditionally call the "activate" method when the user presses the Enter key?

I was hoping the following would work, but it doesn't:

<input type="text" v-model="code" v-on:keyup.enter="code.match(/^\d{6}$/) ? activate : null">

methods: {
    activate() {
        ...
    },

P粉394812277P粉394812277243 days ago435

reply all(1)I'll reply

  • P粉425119739

    P粉4251197392024-03-27 15:01:11

    Try this method. Move the logic to another method and call activation from that method if the conditions for calling activation are met.

    
        
        methods: {
            keyPressed() {
                if(this.code.match(/^\d{6}$/)){
                   activate() 
                }
            },
            activate() {
              ...
             }
        }

    reply
    0
  • Cancelreply