vue父子通讯

文章介绍了在Vue.js中父子组件之间进行交互的几种方式,包括子组件调用父组件的方法($parent引用,$emit触发事件),父组件调用子组件的方法(使用ref),以及父子组件之间的数据传递(props和$emit)。这些机制使得组件间的通信更加灵活。
摘要由CSDN通过智能技术生成

子组件调用父组件中的方法

我把子组件注册成了全局组件,所以在父页面中不用引入直接使用就可以了。

import son from '@/components/son'
Vue.component(son.name,son)

方法一 ,使用this.$parent.event

 父组件

<template>
  <div>
    <son></son>
  </div>
</template>

<script>
export default {
    methods:{
        father() {
            console.log('我是父组件')
        }
    }
}
</script>

子组件

<template>
  <div>
    <button @click="son">点击</button>
  </div>
</template>

<script>
export default {
    name:'son',
    methods:{
        son() {
            this.$parent.father()
        }
    }
}
</script>

方法二 ,使用$emit

父组件

<template>
  <div>
    <son @father="father"></son>
  </div>
</template>

<script>
export default {
    methods:{
        father() {
            console.log('我是父组件')
        }
    }
}
</script>

子组件

<template>
  <div>
    <button @click="son">点击</button>
  </div>
</template>

<script>
export default {
    name:'son',
    methods:{
        son() {
            this.$emit('father')
        }
    }
}
</script>

方法三,使用props 

父组件

<template>
  <div>
    <son :father="father"></son>
  </div>
</template>
<script>

export default {
    methods:{
        father() {
            console.log('我是父组件')
        }
    }
}
</script>

子组件

<template>
  <div>
    <button @click="son">点击</button>
  </div>
</template>

<script>
export default {
    name:'son',
    props: {
        father: {
          type: Function,
          default: null
        }
    },
    methods:{
        son() {
            if(this.father) {
              this.father()
            }
        }
    }
}
</script>

 父组件调用子组件中的方法

使用ref

父组件

<template>
  <div>
    <son ref="child"></son>
    <button @click="father">点击</button>
  </div>
</template>

<script>
export default {
    methods:{
        father() {
            this.$refs.child.son()
        }
    }
}
</script>

子组件

<template>
  <div>
    
  </div>
</template>

<script>
export default {
    name:'son',
    methods:{
        son() {
           console.log('我是子组件')
        }
    }
}
</script>

子组件向父组件传值 

使用$emit 

 父组件

<template>
  <div>
    <son @son="father"></son>
    <p>{{name}}</p>
  </div>
</template>

<script>
export default {
    data() {
      return {
        name:''
      }
    },
    methods: {
      father(data) {
        this.name = data
      }
    }
}
</script>

子组件

<template>
  <div>
    <button @click="son">按钮</button>
  </div>
</template>

<script>
export default {
    name:'son',
    data() {
      return {
        name:'我是子组件'
      }
    },
    methods: {
      son() {
        this.$emit("son",this.name)
      }
    }
}
</script>

父组件向子组件传值

使用props

父组件

<template>
  <div>
    <son :name="name"></son>
  </div>
</template>

<script>
export default {
    data() {
      return {
        name:'我是父组件'
      }
    }
}
</script>

 子组件

<template>
  <div>
    {{name}}
  </div>
</template>

<script>
export default {
    name:'son',
    props: {
      name: String,
      required: true
    },
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值