vue3父子组件传值

  1. 父传子

child.vue

<template>
    <div>父组件内容:{{info}}</div>
</template>

<script>

export default {
  name: 'home',
  components: {
  },
  props: {
    'info': String
  },
  computed: {
  },
  data() {
    return {
        
    }
  },
  mounted() {
    
  },
  methods: {
  },
}
</script>
  
  
<style scoped>
</style>

father.vue

<template>
    <child :info="info_"/>
</template>

<script >
import child from './views/child.vue';
export default {
  name: 'App',
  components: {
    child
  },
  data() {
    return {
      info_: "父组件数据",
    }
  },
  methods: {
  },
}
</script>
  
<style scoped>
</style>
  1. 子传父

child.vue

<template>
</template>

<script>

export default {
  name: 'home',
  components: {
  },
  props: {
  },
  computed: {
  },
  data() {
    return {
        is_ok:false,  //本页面是否加载完成
    }
  },
  mounted() {
    this.is_ok = true
    this.father()
  },
  methods: {
    father(){
      this.$emit('child_',this.is_ok)
      console.log('child is_ok', this.is_ok)
    },
    }
}
</script>
  
  
<style scoped>
</style>

father.vue

<template>
    <child @child_="child_"/>
</template>

<script >
import child from './components/child.vue';
export default {
  name: 'App',
  components: {
    child
  },
  data() {
    return {
      child_ok: false,
    }
  },
  methods: {
    child_(val){
      console.log('子组件的数据', val)
      if(val == true){
        this.child_ok = true
      }
    },
  },
}
</script>
  
<style scoped>
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值