VueJS 2 - KeyUp doesn't work

2019-08-17 08:36发布

问题:

I use Vuetify to generate input field with:

<v-text-field
  label="Search"
  v-model="search"
  @keyup.enter="search()"
  required
></v-text-field>

I want I can key up enter to search from this field:

search () {
  alert('test')
}

When I key up on enter key, this method doesn't executed...

回答1:

Make sure you use your developer console for debugging so you can see what error messages you are getting:

  • Windows: ctrl+shift+I

  • Mac: +Option+I

The problem you are actually having here is that you have declared search as a data property and as a method, so you should see the following message:

[Vue warn]: Method "search" has already been defined as a data property.

To fix this change you method name or your data property name:

new Vue({
  el: '#app',
  methods: {
    search() {
      alert('search')
    },
  },
  data: {
    searchTerm: ''
  }
})

And you should find it works fine.

Here's the JSFiddle: https://jsfiddle.net/er9wsfcy/



回答2:

I had the same issue and everything was in order, turns out the Browser that i was using was the one with issues.

You can try viewing your console from a different browser eg Chrome.

Maybe this might help someone;