版本
"mitt": "^3.0.0",
"save": "^2.4.0",
"vue": "^3.0.0"
错误如下
Failed to compile.
src/components/ValidateForm.vue:37:5
TS2769: No overload matches this call.
Overload 1 of 2, '(type: "*", handler: WildcardHandler<Record<EventType, unknown>>): void', gave the following error.
Argument of type '"form-item-created"' is not assignable to parameter of type '"*"'.
Overload 2 of 2, '(type: "form-item-created", handler: Handler<unknown>): void', gave the following error.
Argument of type '(test: string) => void' is not assignable to parameter of type 'Handler<unknown>'.
Types of parameters 'test' and 'event' are incompatible.
Type 'unknown' is not assignable to type 'string'.
35 | */
36 |
> 37 | emitter.on('form-item-created', callback)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38 | // emitter.on('form-item-created', e => console.log('form-item-created', e))
39 | onUnmounted(() => {
40 | emitter.off('form-item-created', e => console.log('form-item-created', e))
部分代码
import mitt from 'mitt'
type ValidateFunc = () => boolean
export const emitter = mitt()
export default defineComponent({
name: 'ValidateForm',
emits: ['form-submit'],
setup (props, context) {
const submitForm = () => {
const result = funcArr.map(func => func()).every(result => result)
context.emit('form-submit', result)
}
const callback = (test: string) => {
console.log(test)
}
emitter.on('form-item-created', callback)
// emitter.on('form-item-created', e => console.log('form-item-created', e))
onUnmounted(() => {
emitter.off('form-item-created', e => console.log('form-item-created', e))
})
emitter.on 调用自己写的handler: callback 编译出错,如果用官网给的(比如注掉的代码)
就可以运行
--
FROM 45.51.104.*