使用vue框架进行父子组件传值的时候,可能会出现如下警告:
Extraneous non-emits event listeners (XXX) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the "emits" option.
无关的非发出事件侦听器(XXX)被传递给组件,但不能自动继承,因为组件呈现片段或文本根节点。如果侦听器只打算作为组件自定义事件侦听器,则使用"emit "选项声明它。
XXX是子传父的自定义事件名称
详情如下所示:
原因:
https://github.com/vuejs/core/issues/2540
解决办法:
在需要传递事件的组件中,新增如下代码:
emits: ['XXX'] // 和props是同一级
文章讲述了在Vue中,当父子组件试图通过非标准方式传递事件时会触发警告。解决方法是在子组件中通过`emits`选项显式声明自定义事件名,避免因渲染结构导致的自动继承问题。参考了GitHubissue#2540。


1万+

被折叠的 条评论
为什么被折叠?



