简体   繁体   中英

Functional component emit event: Expression is not callable - Vue

Functional component with render function:

render(createElement, { listeners } {
  return createElement('div', {
    on: {
      click: () => {
        // issue is on this line
        const onItemClick: any = listeners['item-click']
        onItemClick(1, 2, 3)
      }
    }
  })
}

How to correctly type onItemClick so i won't get an error:

const onItemClick: Function | Function[]
This expression is not callable.
  No constituent of type 'Function | Function[]' is callable.ts(2349)

Signature of listeners :

var listeners: {
    [key: string]: Function | Function[];
}

Solved it using type assertion:

const onItemClick = listeners['item-click'] as (val1: string, val2: string) => void
onItemClick('123, '456')

Try replacing the arrow function with the function() constructor.

on: { click: function() { // do the stuff }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM