Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

组件上传递的事件会自动绑定到组件内部所有元素上 #457

Closed
wants to merge 5 commits into from
Closed

Conversation

zzzz-Z
Copy link
Contributor

@zzzz-Z zzzz-Z commented Nov 14, 2019

组件内部并没有主动触发外部绑定的事件,但是事件会自动绑定到所有子元素上。
packages/vue/examples/todomvc-composition.html
packages/runtime-core/tests/apiSetupContext.spec.ts
很奇怪 , 测试用例里是没有问题的。但是在实际使用过程中会出现。

@yyx990803
Copy link
Member

DOM 里面事件是会冒泡的... 你点击子元素当然会出发父元素的 click 事件了

@yyx990803 yyx990803 closed this Nov 14, 2019
@zzzz-Z
Copy link
Contributor Author

zzzz-Z commented Nov 14, 2019

可是我并没有在父元素上绑定事件啊,组件外部传递的事件不应该主动绑定到根元素上吧
2.0也不是这样子的啊

@zzzz-Z
Copy link
Contributor Author

zzzz-Z commented Nov 14, 2019

而且测试用例和实际使用情况展现的完全两种情况 这也是不应该的啊

@Justineo
Copy link
Member

@yyx990803 这好像说的是没有 .native 的时候 <child @click="..."> 绑到了它的根元素上?

@zzzz-Z
Copy link
Contributor Author

zzzz-Z commented Nov 14, 2019

@Justineo @yyx990803 对就是想说这个。 2.0的时候只有在.native的时候才会添加到根元素上

@fnlctrl
Copy link
Member

fnlctrl commented Nov 14, 2019

新的attr fallthrough rfc里移除了.native vuejs/rfcs#92

@zzzz-Z
Copy link
Contributor Author

zzzz-Z commented Nov 14, 2019

@fnlctrl 谢谢 这么说以后都得加上 inheritAttrs: false,
除非你想让所有事件都继承到根元素上 是这个样子吗?

@fnlctrl
Copy link
Member

fnlctrl commented Nov 14, 2019

要等这个rfc最终敲定了才能确定,也有可能有变动。目前的实现是这个行为。

@haoqunjiang
Copy link
Member

而且测试用例和实际使用情况展现的完全两种情况 这也是不应该的啊

这是因为 runtime-test 没有实现冒泡。放到 runtime-dom 里的测试应该就一致了。

@yyx990803
Copy link
Member

yyx990803 commented Nov 14, 2019

简单来说,以后默认所有没有被声明为 props 的属性(包括 class, style, 事件和普通的 attribute)都会被子组件的根元素继承。如果想要订制这个行为,就用 inhertiAttrs: false 然后自己在子元素里绑定 $attrs

@theniceangel
Copy link
Contributor

简单来说,以后默认所有没有被声明为 props 的属性(包括 class, style, 事件和普通的 attribute)都会被子组件的根元素继承。如果想要订制这个行为,就用 inhertiAttrs: false 然后自己在子元素里绑定 $attrs

应该是 inheritAttrs: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants