$scopedSlots, $props, $attrs, $listeners

$scopedSlots

因为父页面使用了 header slot 所以会有打印
因为父页面没有使用了 footer slot 所以会有打印undefined

image.png

parent 页面





child 页面






$attrs, $props, $listeners

image.png

$attrs:将父页面的 v-bind绑定数据穿透传递至下级页面,如果你在某一个组件中注册了props 那么后续的组件就无法通过$attrs 来获取你已经注册过props的属性

$props:当此页面有注册的props 你可以在 template 中 使用 $props 来获取你注册的props属性

$listeners:使$emit 可以跨越更多的层级,也可以在中间的组件中进行监听,让每一个组件都可以监听到事件

注意
$attrs是可以在 template与 script 中使用的
$props 只可以在 template 中 使用

$attrs与$props可以使用在三级组件通信, 如果更多 还是推荐使用 provide / inject 来传递

下面是以上三种API的demo

page 页面







Parent 页面







Son 页面







GreatGrandson 页面







你可能感兴趣的:($scopedSlots, $props, $attrs, $listeners)