父子组件的通信和函数调用

父组件向子组件传递数据

<template>
  <div>
    <!-- 使用子组件并传递数据 -->
    <children :mes="message" :fruitlist="fruit"></children>
  </div>
</template>

<script>
import children from '../components/children/children.vue'   //引入子组件
export default {
  name: 'parent',
  data () {
    return {
      message: '你好',
      fruit: ['苹果','香蕉','橘子','菠萝','桃子'],
    };
  },
  components: {
    children          //注册子组件
  }
}
</script>
<template>
  <div>
    <h2>{{mes}}</h2>
    <h2>{{fruitlist}}</h2>
  </div>
</template>

<script>
export default {
  name: 'children',
  data () {
    return {
    };
  },
  props: {                  //接收父组件传递的值
    mes: String,            //传递的参数名和参数类型
    fruitlist: {
      type: Array,          //类型 
      default: '没有水果',   //默认值:没有传参时显示默认值
      required: true        //是否为必传值
    } 
  }
}
</script>

子组件向父组件传递数据

<template>
  <div>
    <button v-for="item in fruit" :key="item" @click="send(item)">{{item}}</button>
  </div>
</template>

<script>
export default {
  name: 'children',
  data () {
    return {
      fruit: ['苹果','香蕉','橘子','菠萝','桃子'],
    };
  },
  methods: {
    send(item) {
      //子组件发送事件(事件名,传递的参数)
      this.$emit('item-click',item)
    }
  },
}
</script>
<template>
  <div>
    <h2>子组件中点击的按钮:{{fruit}}</h2>
    <children @item-click='get'/>
  </div>
</template>

<script>
import children from '../components/children/children.vue'   //引入子组件
export default {
  name: 'parent',
  data () {
    return {
        fruit: ''
    };
  },
  components: {
    children          //注册子组件
  },
  methods: {
    get(item) {      //父组件定义方法接收子组件传递的参数
      this.fruit = item
    }
  },
}
</script>

父子组件的访问

  • 父组件访问子组件:使用$refs/children (不推荐使用children)
  • 子组件访问父组件:使用$parent (不建议使用)
<template>
 <div>
   <children ref="cpn"></children>   <!-- ref=" "相当于给子组件取了一个名字,调用时使用这个名字 -->
   <button @click="getfruit">获取子组件的水果列表</button>
   <button @click="getaaa">调用子组件的函数aaa</button>
   <button @click="getbbb">调用子组件的函数bbb</button>
 </div>
</template>

<script>
import children from '../components/children/children.vue'   //引入子组件
export default {
 name: 'parent',
 data () {
   return {
       fruit: ''
   };
 },
 components: {
   children          //注册子组件
 },
 methods: {
   getfruit() {
   //   console.log(this.$children[0].fruit);
     console.log(this.$refs.cpn.fruit);
   },
   getaaa() {
   //   this.$children[0].aaa()
     this.$refs.cpn.aaa()
   },
   getbbb() {
   //   this.$children[0].bbb()
     this.$refs.cpn.bbb()
   }
 },
}
</script>
<template>
  <div>
    <button @click="getccc">调用父组件的函数ccc</button>
  </div>
</template>

<script>
export default {
  name: 'children',
  data () {
    return {
      fruit: ['苹果','香蕉','橘子','菠萝','桃子'],
    };
  },
  methods: {
    aaa() {
      console.log("aaa函数被调用");
    },
    bbb() {
      console.log("bbb函数被调用");
    },
    getccc() {
      this.$parent.ccc()
    }
  },
}
</script>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呦呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值