Vue3父子组件间传值

在vue3中,子组件使用父组件值的方式发生了变化,用props接收后在setup中是无法通过this.xxx调用的。

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

父传子


   
   
    
    
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script>
  9. import son from './components/son.vue'
  10. export default {
  11. components: { son },
  12. data () {
  13. return {
  14. parentValue: '父传给子的值'
  15. }
  16. }
  17. }
  18. </script>

子组件通过props接收


   
   
    
    
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: [ 'value']
  10. }
  11. </script>

子传父


   
   
    
    
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <div>{ { parentValue }} </div>
  5. <hr />
  6. <son @handle="handel"> </son>
  7. </div>
  8. </template>
  9. <script>
  10. import son from './components/son.vue'
  11. export default {
  12. components: { son },
  13. data () {
  14. return {
  15. parentValue: '父传给子的值'
  16. }
  17. },
  18. methods: {
  19. handel (value) {
  20. this. parentValue = value
  21. }
  22. }
  23. }
  24. </script>

子组件通过$emit触发父组件中的事件传值给父组件


   
   
    
    
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <button @click="fn">点击传值给父组件 </button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. fn () {
  11. this.$emit( 'handle', '子传给父的值')
  12. }
  13. }
  14. }
  15. </script>

接下来看看vue3的传值

父传子


   
   
    
    
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref } from 'vue'
  10. import son from './components/son.vue'
  11. export default defineComponent({
  12. components: { son },
  13. setup () {
  14. const parentValue = ref( '父组件传给子组件的值')
  15. return { parentValue }
  16. }
  17. })
  18. </script>

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


   
   
    
    
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent } from 'vue'
  9. export default defineComponent({
  10. props: [ 'value'],
  11. setup (props, context) {
  12. console. log(props. value) // 父组件传给子组件的值
  13. }
  14. })
  15. </script>

子传父


   
   
    
    
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. { { parentValue }}
  5. <hr />
  6. <son @handle="handle"> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. let parentValue = ref( '父组件传给子组件的值')
  16. const handle = value => {
  17. parentValue. value = value
  18. }
  19. return { parentValue, handle }
  20. }
  21. })
  22. </script>

子通过emit触发父组件中的方法传值


   
   
    
    
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <button @click="fn">点击改变父组件的值 </button>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, ref } from 'vue'
  9. export default defineComponent({
  10. setup (props, context) {
  11. const fn = ( ) => {
  12. context. emit( 'handle', '子组件传给父组件的值')
  13. }
  14. return { fn }
  15. }
  16. })
  17. </script>

provide和inject 

vue3还给我们提供了provide和inject,我们可以在父组件中通过provide暴露属性或方法,子组件通过inject接受,并且只要是父组件的后代,都可以通过inject接收到父组件暴露的值


   
   
    
    
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. { { parentValue }}
  5. <hr />
  6. <son> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, provide, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. // 传值
  16. let parentValue = ref( '父中的值')
  17. provide( 'parentValue', parentValue)
  18. // 传方法
  19. const handle = ( sonValue: any) => {
  20. parentValue. value = sonValue
  21. }
  22. provide( 'handle', handle)
  23. return { parentValue }
  24. }
  25. })
  26. </script>

   
   
    
    
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. <button @click="fn">点击修改父组件的值 </button>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, inject } from 'vue'
  10. export default defineComponent({
  11. setup () {
  12. const value = inject( 'parentValue') // 使用父组件传来的值
  13. const handle: any = inject( 'handle')
  14. // 修改父组件的值
  15. const fn = ( ) => {
  16. handle( '修改父组件的值')
  17. }
  18. return { value, fn }
  19. }
  20. })
  21. </script>

在vue3中,子组件使用父组件值的方式发生了变化,用props接收后在setup中是无法通过this.xxx调用的。

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

父传子


   
   
  
  
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script>
  9. import son from './components/son.vue'
  10. export default {
  11. components: { son },
  12. data () {
  13. return {
  14. parentValue: '父传给子的值'
  15. }
  16. }
  17. }
  18. </script>

子组件通过props接收


   
   
  
  
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: [ 'value']
  10. }
  11. </script>

子传父


   
   
  
  
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <div>{ { parentValue }} </div>
  5. <hr />
  6. <son @handle="handel"> </son>
  7. </div>
  8. </template>
  9. <script>
  10. import son from './components/son.vue'
  11. export default {
  12. components: { son },
  13. data () {
  14. return {
  15. parentValue: '父传给子的值'
  16. }
  17. },
  18. methods: {
  19. handel (value) {
  20. this. parentValue = value
  21. }
  22. }
  23. }
  24. </script>

子组件通过$emit触发父组件中的事件传值给父组件


   
   
  
  
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <button @click="fn">点击传值给父组件 </button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. fn () {
  11. this.$emit( 'handle', '子传给父的值')
  12. }
  13. }
  14. }
  15. </script>

接下来看看vue3的传值

父传子


   
   
  
  
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. <hr />
  5. <son :value="parentValue"> </son>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref } from 'vue'
  10. import son from './components/son.vue'
  11. export default defineComponent({
  12. components: { son },
  13. setup () {
  14. const parentValue = ref( '父组件传给子组件的值')
  15. return { parentValue }
  16. }
  17. })
  18. </script>

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


   
   
  
  
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent } from 'vue'
  9. export default defineComponent({
  10. props: [ 'value'],
  11. setup (props, context) {
  12. console. log(props. value) // 父组件传给子组件的值
  13. }
  14. })
  15. </script>

子传父


   
   
  
  
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. { { parentValue }}
  5. <hr />
  6. <son @handle="handle"> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. let parentValue = ref( '父组件传给子组件的值')
  16. const handle = value => {
  17. parentValue. value = value
  18. }
  19. return { parentValue, handle }
  20. }
  21. })
  22. </script>

子通过emit触发父组件中的方法传值


   
   
  
  
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <button @click="fn">点击改变父组件的值 </button>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, ref } from 'vue'
  9. export default defineComponent({
  10. setup (props, context) {
  11. const fn = ( ) => {
  12. context. emit( 'handle', '子组件传给父组件的值')
  13. }
  14. return { fn }
  15. }
  16. })
  17. </script>

provide和inject 

vue3还给我们提供了provide和inject,我们可以在父组件中通过provide暴露属性或方法,子组件通过inject接受,并且只要是父组件的后代,都可以通过inject接收到父组件暴露的值


   
   
  
  
  1. <template>
  2. <div>
  3. <h1>父组件 </h1>
  4. { { parentValue }}
  5. <hr />
  6. <son> </son>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, provide, ref } from 'vue'
  11. import son from './components/son.vue'
  12. export default defineComponent({
  13. components: { son },
  14. setup () {
  15. // 传值
  16. let parentValue = ref( '父中的值')
  17. provide( 'parentValue', parentValue)
  18. // 传方法
  19. const handle = ( sonValue: any) => {
  20. parentValue. value = sonValue
  21. }
  22. provide( 'handle', handle)
  23. return { parentValue }
  24. }
  25. })
  26. </script>

   
   
  
  
  1. <template>
  2. <div>
  3. <p>子组件 </p>
  4. <div>{ { value }} </div>
  5. <button @click="fn">点击修改父组件的值 </button>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, inject } from 'vue'
  10. export default defineComponent({
  11. setup () {
  12. const value = inject( 'parentValue') // 使用父组件传来的值
  13. const handle: any = inject( 'handle')
  14. // 修改父组件的值
  15. const fn = ( ) => {
  16. handle( '修改父组件的值')
  17. }
  18. return { value, fn }
  19. }
  20. })
  21. </script>

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034421