vue3.x 常见的组件通信方法
1、 props
父子组件传参,父组件通过自定义属性传参,子组件props接收参数(具体代码如下)
2、$emit、$parent、$refs | $children(已废除)
emit子-->父触发自定义事件传递参数,与2.x基本一致,3.x访问emit通过getCurrentInstance的props
parent-->与2.x基本一致,3.x访问parent通过getCurrentInstance的props
refs-->与2.x基本一致,3.x访问refs通过getCurrentInstance的props
3、 provide、inject
向深层次组件传递参数,向组件的子组件、孙组件......传参
4、 mitt
mitt,类似于2.x的eventBus,需单独下载依赖包,使用方法如下,与eventBus类似
5、 vuex
与2.x基本一致,3.x访问vuex通过useStore或者getCurrentInstance的props
6、 路由传参
与2.x基本一致,3.x访问路由通过useRouter或者getCurrentInstance的props