【Vue知识点】 vue组件之间如何传值

1、组件 组件

父组件:

子组件:

         props:['msg']

         props:{

              msg:数据类型

         }

子组件props的内容为冒号后面的内容不是引号里面的内容,一般来说是:xxx='msg' ,props:['xxx']  ,为了方便一般冒号和引号里的内容一致

2、组件    组件

             

子组件:this.$emit("childInput",this.changeVal);

父组件:

           

           methods:{

                getVal(msg){
                     //msg就是  子组件传递的数据

                }

           }

子组件的第一个参数为自定义事件的名称第二个参数为要传的数据

3、兄弟组件之间的传值

通过一个中转(事件总线bus)

A兄弟传值:

         import bus from '@/common/bus'

         bus.$emit("toFooter",this.msg)

B兄弟接收:

         import bus from '@/common/bus'

         bus.$on('toFooter',(data)=>{

             this.str=data

         })

//bus.js 
import Vue from 'vue';
export default new Vue;
//使用 A兄弟 传值
import bus from '路径'
bus.$emit('自定义事件名称',输出数据)
//使用 B兄弟 接收
import bus from '路径'
bus.on('自定义事件名',(res)=>{})

你可能感兴趣的:(Vue,vue.js,前端,javascript)