Vue祖孙组件传值

祖传孙

方法一:props

传递次序:GrandFather → Father → GrandSon

1、在 GrandFather 中给 Father 传递了一条消息。

GrandFather.vue

<template>
  <div class="parent">
    🎈爷爷
    <Father :msg1="msg1" :msg2="msg2" />
  </div>
</template>

<script>
import Father from './Father'
export default {
  components: {Father},
  data () {
    return {
      msg1: '1️⃣我是GrandFather,把第二条传给GrandSon',
      msg2: '2️⃣GrandSon你好,我是GrandFather'
    }
  }
}
</script>

2、Father 中使用 props 接收了来自 GrandFather 的所有消息,把消息通过props传给 GrandSon
Father.vue

<template>
  <div class="parent">
    🎈父亲
    <p>GrandFather说:{{msg1}}{{msg2}}</p>
    <GrandSon :msg2="msg2" />
  </div>
</template>

<script>
import GrandSon from './GrandSon'
export default {
  props: ['msg1', 'msg2'],
  components: {GrandSon},
}
</script>

3、终于到 GrandSon 了,它通过 props 从 Father 那里接收到了来自 GrandFather 的消息
GrandSon.vue

<template>
<div class="child">
  🎈孙子
  <p>GrandFather说:{{msg2}}</p>
</div>
</template>

<script>
export default {
  props: ['msg2']
}
</script>

在这里插入图片描述
方式二:$attrs
上面的 $props 传值方式必须要经过 Father 接收之后继续传递,不方便。

vue 在 2.4.0 版本中新增了 $attrs 属性。根据前面的理解 a t t r s 就 是 没 有 在 p r o p s 中 声 明 要 接 收 的 一 些 属 性 。 此 外 , 还 可 以 通 过 v − b i n d = " attrs 就是没有在 props 中声明要接收的一些属性。此外,还可以通过 v-bind=" attrspropsvbind="attrs" 把来自父组件的一些属性直接传递到子组件中。

这样一来,Father组件就没必要在 props 中声明接收那些不必要属性了。看看实例吧!

① GrandFather 组件不用做修改

② 这次在 Father 中只在 props 接收了 msg1,与自己无关的直接使用 v-bind=“attrs” 绑定到子组件上。

当然,在 Father 中还是可以访问 a t t r s 的 。 在 代 码 中 访 问 要 使 用 t h i s . attrs 的。在代码中访问要使用 this. attrs访使this.attrs
Father.vue

<template>
  <div class="parent">
    🎈父亲
    <p>$attrs:{{$attrs}}</p>
    <GrandSon v-bind="$attrs" />
  </div>
</template>

<script>
import GrandSon from './GrandSon'
export default {
  props: ['msg1'], //只接收了msg1
  components: {GrandSon},
}
</script>

在这里插入图片描述

孙传祖

$listeners
给 Father 组件绑定 自定义事件 getReply ,便于后面在 GrandSon 中触发

GrandFather.vue

<template>
  <div class="parent">
    🎈爷爷
    <div>GrandSon的回复:{{reply}}</div>
    <Father :msg1="msg1" :msg2="msg2" @getReply="getReply"/>
  </div>
</template>

<script>
import Father from './Father'
export default {
  components: {Father},
  data () {
    return {
      msg1: '1️⃣我是GrandFather,把第二条传给GrandSon',
      msg2: '2️⃣GrandSon你好,我是GrandFather',
      reply: '' //接收来自GrandSon的消息
    }
  },
  methods: {
    /* 将获得的数据绑定到data中,便于视图层渲染 */
    getReply (param) {
      this.reply = param
    }
  }
}
</script>

在 Father 中使用 v-on="$listeners" 把 GrandFather 的事件绑定到 GrandSon

Father.vue

<template>
  <div class="parent">
    🎈父亲
    <p>$attrs:{{$attrs}}</p>
    <GrandSon v-bind="$attrs" v-on="$listeners" />
  </div>
</template>

<script>
import GrandSon from './GrandSon'
export default {
  props: ['msg1'],
  components: { GrandSon },
}
</script>

在 GrandSon 中触发来自 GrandFather 的自定义事件就 🆗 了,有两种方式。

① this.$ listeners.eventName(param)
② this.$emit(eventName, param)

GrandSon.vue

<template>
<div class="child">
  🎈孙子
  <p>GrandFather说:{{msg2}}</p>
  <button @click="reply">回复GrandFather</button>
</div>
</template>

<script>
export default {
  props: ['msg2'],
  data () {
    return {
      replyWord: 'GrandFather你好,我是GrandSon,收到消息了'
    }
  },
  methods: {
    reply () {
      this.$emit('getReply', this.replyWord)
      // this.$listeners.getReply(this.replyWord)
    }
  }
}
</script>

在这里插入图片描述

  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值