File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed
Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -92,7 +92,7 @@ describe('component: emit', () => {
9292 } )
9393 render ( h ( Foo ) , nodeOps . createElement ( 'div' ) )
9494 expect (
95- `Component emitted event "bar" but it is not declared`
95+ `Component emitted event "bar" but it is neither declared`
9696 ) . toHaveBeenWarned ( )
9797 } )
9898
@@ -109,10 +109,26 @@ describe('component: emit', () => {
109109 } )
110110 render ( h ( Foo ) , nodeOps . createElement ( 'div' ) )
111111 expect (
112- `Component emitted event "bar" but it is not declared`
112+ `Component emitted event "bar" but it is neither declared`
113113 ) . toHaveBeenWarned ( )
114114 } )
115115
116+ test ( 'should not warn if has equivalent onXXX prop' , ( ) => {
117+ const Foo = defineComponent ( {
118+ props : [ 'onFoo' ] ,
119+ emits : [ ] ,
120+ render ( ) { } ,
121+ created ( ) {
122+ // @ts -ignore
123+ this . $emit ( 'foo' )
124+ }
125+ } )
126+ render ( h ( Foo ) , nodeOps . createElement ( 'div' ) )
127+ expect (
128+ `Component emitted event "bar" but it is neither declared`
129+ ) . not . toHaveBeenWarned ( )
130+ } )
131+
116132 test ( 'validator warning' , ( ) => {
117133 const Foo = defineComponent ( {
118134 emits : {
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111import { ComponentInternalInstance } from './component'
1212import { callWithAsyncErrorHandling , ErrorCodes } from './errorHandling'
1313import { warn } from './warning'
14+ import { normalizePropsOptions } from './componentProps'
1415
1516export type ObjectEmitsOptions = Record <
1617 string ,
@@ -48,10 +49,13 @@ export function emit(
4849 const options = normalizeEmitsOptions ( instance . type . emits )
4950 if ( options ) {
5051 if ( ! ( event in options ) ) {
51- warn (
52- `Component emitted event "${ event } " but it is not declared in the ` +
53- `emits option.`
54- )
52+ const propsOptions = normalizePropsOptions ( instance . type . props ) [ 0 ]
53+ if ( ! propsOptions || ! ( `on` + capitalize ( event ) in propsOptions ) ) {
54+ warn (
55+ `Component emitted event "${ event } " but it is neither declared in ` +
56+ `the emits option nor as an "on${ capitalize ( event ) } " prop.`
57+ )
58+ }
5559 } else {
5660 const validator = options [ event ]
5761 if ( isFunction ( validator ) ) {
You can’t perform that action at this time.
0 commit comments