vue2父子/子父传参 vue3父子/子父传参

 

让我们先来看看vue2父子间的传值
父传子

<template>
  <div>
    <h1>父组件</h1>
    <hr />
    <son :value="parentValue"></son>
  </div>
</template>
<script>
import son from './components/son.vue'
export default {
  components: { son },
  data () {
    return {
      parentValue: '父传给子的值'
    }
  }
}
</script>
子组件通过props接收

<template>
  <div>
    <p>子组件</p>
    <div>{{ value }}</div>
  </div>
</template>
<script>
export default {
  props: ['value']
}
</script>
子传父

<template>
  <div>
    <h1>父组件</h1>
    <div>{{ parentValue }}</div>
    <hr />
    <son @handle="handel"></son>
  </div>
</template>
<script>
import son from './components/son.vue'
export default {
  components: { son },
  data () {
    return {
      parentValue: '父传给子的值'
    }
  },
  methods: {
    handel (value) {
      this.parentValue = value
    }
  }
}
</script>
子组件通过$emit触发父组件中的事件传值给父组件

<template>
  <div>
    <p>子组件</p>
    <button @click="fn">点击传值给父组件</button>
  </div>
</template>
<script>
export default {
  methods: {
    fn () {
      this.$emit('handle', '子传给父的值')
    }
  }
}
</script>

接下来看看vue3的传值
父传子

<template>
  <div>
    <h1>父组件</h1>
    <hr />
    <son :value="parentValue"></son>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import son from './components/son.vue'
 
export default defineComponent({
  components: { son },
  setup () {
    const parentValue = ref('父组件传给子组件的值')
    return { parentValue }
  }
})
</script>

由于vue3的setup中没有this,所以我们想要在setup中使用父组件传来的值,要借助setup函数的第一个参数

<template>
  <div>
    <p>子组件</p>
    <div>{{ value }}</div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
 
export default defineComponent({
  props: ['value'],
  setup (props, context) {
    console.log(props.value) // 父组件传给子组件的值
  }
})
</script>
子传父

<template>
  <div>
    <h1>父组件</h1>
    {{ parentValue }}
    <hr />
    <son @handle="handle"></son>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import son from './components/son.vue'
 
export default defineComponent({
  components: { son },
  setup () {
    let parentValue = ref('父组件传给子组件的值')
    const handle = value => {
      parentValue.value = value
    }
    return { parentValue, handle }
  }
})
</script>
子通过emit触发父组件中的方法传值

<template>
  <div>
    <p>子组件</p>
    <button @click="fn">点击改变父组件的值</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
 
export default defineComponent({
  setup (props, context) {
    const fn = () => {
      context.emit('handle', '子组件传给父组件的值')
    }
    return { fn }
  }
})
</script>
provide和inject 
vue3还给我们提供了provide和inject,我们可以在父组件中通过provide暴露属性或方法,子组件通过inject接受,并且只要是父组件的后代,都可以通过inject接收到父组件暴露的值

<template>
  <div>
    <h1>父组件</h1>
    {{ parentValue }}
    <hr />
    <son></son>
  </div>
</template>
<script lang="ts">
import { defineComponent, provide, ref } from 'vue'
import son from './components/son.vue'
 
export default defineComponent({
  components: { son },
  setup () {
    // 传值
    let parentValue = ref('父中的值')
    provide('parentValue', parentValue)
    // 传方法
    const handle = (sonValue: any) => {
      parentValue.value = sonValue
    }
    provide('handle', handle)
    return { parentValue }
  }
})
</script>
<template>
  <div>
    <p>子组件</p>
    <div>{{ value }}</div>
    <button @click="fn">点击修改父组件的值</button>
  </div>
</template>
<script lang="ts">
import { defineComponent, inject } from 'vue'
 
export default defineComponent({
  setup () {
    const value = inject('parentValue') // 使用父组件传来的值
    const handle: any = inject('handle')
    // 修改父组件的值
    const fn = () => {
      handle('修改父组件的值')
    }
    return { value, fn }
  }
})

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值