Home  >  Q&A  >  body text

Use Options API to handle signals emitted by vue 3

Is there a way to enter emissions in the vue 3 Options API similar to the Composition API? According to the combination (documentation):

<script setup lang="ts">
// type-based
const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()
</script>

But for the options API we only have payload validation (documentation):

emits: {
    addBook(payload: { bookName: string }) {
      // perform runtime validation
      return payload.bookName.length > 0
    }
  }

So if we don't need validation, eslint will treat the parameter as unused:

emits: {
    change: (id: number) => true // 'id' is defined but never used
}


P粉994092873P粉994092873292 days ago436

reply all(1)I'll reply

  • P粉811349112

    P粉8113491122024-01-01 14:50:31

    Add ignore comment to suppress warning:

    // eslint-disable-next-line no-unused-vars @typescript-eslint/no-unused-vars
    change: (id: number) => true
    

    Or perform simple verification on id:

    change: (id: number) => typeof id === 'number'
    

    reply
    0
  • Cancelreply