Vue3父子組件通信

一、Vue3父子組件通信v-model

在Vue3中,v-model不再是語法糖,而是一個API。v-model指令等價於將value prop和input事件綁定到組件上。通過在組件內部添加model選項,讓組件支持v-model。

//子組件
<template>
  <input :value="value" @input="$emit('input', $event.target.value)" />
</template>

<script>
export default {
  props: {
    value: String
  },
  model: {
    prop: 'value',
    event: 'input'
  }
}
</script>

//父組件
<template>
  <child-component v-model="message" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      message: ''
    }
  }
}
</script>

二、Vue3父子組件通信子組件如何接收

Vue3中的父子組件通信,仍然可以使用props互相通信。子組件需要通過props接收從父組件傳來的數據。在Vue3中,為了避免props的類型錯誤和必需性問題,可以使用標籤中的屬性validation選項,進行數據驗證。

//子組件
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
}
</script>

//父組件
<template>
  <child-component :message="parentMessage" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      parentMessage: 'Hello, child component!'
    }
  }
}
</script>

三、Vue3父子組件方法調用

Vue3中,父組件可以通過$refs或$children屬性來調用子組件中的方法。方法需要在子組件中暴露出來,以便父組件可以訪問到。

//子組件
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  },
  methods: {
    greet() {
      console.log('Hello from the child component!');
    }
  }
}
</script>

//父組件
<template>
  <child-component ref="child" :message="parentMessage" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      parentMessage: 'Hello, child component!'
    }
  },
  mounted() {
    this.$refs.child.greet();
  }
}
</script>

四、Vue父子組件通信規則

在Vue中,組件的通信遵循一定的規則。

  • 父組件可以向子組件傳遞props
  • 子組件可以向父組件觸發事件
  • 父組件可以通過$refs或$children屬性操作子組件
  • 兄弟組件之間需要通過共同的父組件傳遞props或觸發事件進行通信

五、Vue3父子組件雙向綁定

除了v-model,Vue3組件中也可以使用自定義事件以及prop的形式來實現雙向綁定。

//子組件
<template>
  <input :value="message" @input="handleChange" />
</template>

<script>
export default {
  props: {
    message: String
  },
  methods: {
    handleChange(event) {
      this.$emit('update:message', event.target.value);
    }
  }
}
</script>

//父組件
<template>
  <child-component :message="message" @update:message="handleMessageUpdate" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      message: ''
    }
  },
  methods: {
    handleMessageUpdate(value) {
      this.message = value;
    }
  }
}
</script>

六、Vue2父子組件通信

在Vue2中,父子組件通信仍然可以使用props和$emit來實現。

//子組件
<template>
  <button @click="handleClick">{{ buttonText }}</button>
</template>

<script>
export default {
  props: {
    buttonText: String
  },
  methods: {
    handleClick() {
      this.$emit('button-click');
    }
  }
}
</script>

//父組件
<template>
  <child-component :buttonText="buttonText" @button-click="handleButtonClick" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      buttonText: 'Click me!'
    }
  },
  methods: {
    handleButtonClick() {
      console.log('Button clicked!');
    }
  }
}
</script>

七、父子組件通信Vue

在Vue中,父子組件通信是Vue應用中十分常見的情況。

//子組件
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: {
    message: String
  }
}
</script>

//父組件
<template>
  <child-component message="Hello, child component!" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  }
}
</script>

八、Vue子傳父組件通信

在Vue中,子組件可以向父組件傳遞消息,使用$emit觸發事件從子組件向父組件傳遞數據。

//子組件
<template>
  <button @click="handleClick">{{ buttonText }}</button>
</template>

<script>
export default {
  props: {
    buttonText: String
  },
  methods: {
    handleClick() {
      this.$emit('button-click', 'Child component says hello!');
    }
  }
}
</script>

//父組件
<template>
  <child-component :buttonText="buttonText" @button-click="handleButtonClick" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      buttonText: 'Click me!'
    }
  },
  methods: {
    handleButtonClick(message) {
      console.log(message);
    }
  }
}
</script>

九、Vue父子組件生命周期順序

Vue中,組件的生命周期函數在父子組件之間有着不同的順序。

//子組件
<template>
  <p>{{ message }}</p>
</template>

<script>
export default {
  props: {
    message: String
  },
  beforeCreate() {
    console.log('Child component: beforeCreate()');
  },
  created() {
    console.log('Child component: created()');
  },
  beforeMount() {
    console.log('Child component: beforeMount()');
  },
  mounted() {
    console.log('Child component: mounted()');
  },
  beforeUpdate() {
    console.log('Child component: beforeUpdate()');
  },
  updated() {
    console.log('Child component: updated()');
  },
  beforeUnmount() {
    console.log('Child component: beforeUnmount()');
  },
  unmounted() {
    console.log('Child component: unmounted()');
  }
}
</script>

//父組件
<template>
  <child-component message="Hello, child component!" />
</template>

<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  beforeCreate() {
    console.log('Parent component: beforeCreate()');
  },
  created() {
    console.log('Parent component: created()');
  },
  beforeMount() {
    console.log('Parent component: beforeMount()');
  },
  mounted() {
    console.log('Parent component: mounted()');
  },
  beforeUpdate() {
    console.log('Parent component: beforeUpdate()');
  },
  updated() {
    console.log('Parent component: updated()');
  },
  beforeUnmount() {
    console.log('Parent component: beforeUnmount()');
  },
  unmounted() {
    console.log('Parent component: unmounted()');
  }
}
</script>

以上是Vue3父子組件通信的一些細節,希望對大家有所幫助。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/153973.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-15 03:24
下一篇 2024-11-15 03:24

相關推薦

  • 如何修改ant組件的動效為中心

    當我們使用Ant Design時,其默認的組件動效可能不一定符合我們的需求,這時我們需要修改Ant Design組件動效,使其更加符合我們的UI設計。本文將從多個方面詳細闡述如何修…

    編程 2025-04-29
  • Ant Design組件的動效

    Ant Design是一個基於React技術棧的UI組件庫,其中動效是該組件庫中的一個重要特性之一。動效的使用可以讓用戶更清晰、更直觀地了解到UI交互的狀態變化,從而提高用戶的滿意…

    編程 2025-04-29
  • 跨域通信浮標——實現客戶端之間的跨域通信

    本文將介紹跨域通信浮標的使用方法,該浮標可以實現客戶端之間的跨域通信,解決了瀏覽器同源策略的限制,讓開發者能夠更加方便地進行跨域通信。 一、浮標的原理 跨域通信浮標的原理是基於浮動…

    編程 2025-04-27
  • 通信專業Python和Java的開發技巧

    本文旨在介紹通信專業Python和Java的開發技巧,為讀者提供實用且可操作的思路和方法。 一、Python在通信領域中的應用 Python是一種優秀的程序設計語言,因其易學易用、…

    編程 2025-04-27
  • 用mdjs打造高效可復用的Web組件

    本文介紹了一個全能的編程開發工程師如何使用mdjs來打造高效可復用的Web組件。我們將會從多個方面對mdjs做詳細的闡述,讓您輕鬆學習並掌握mdjs的使用。 一、mdjs簡介 md…

    編程 2025-04-27
  • Spring MVC主要組件

    Spring MVC是一個基於Java語言的Web框架,是Spring Framework的一部分。它提供了用於構建Web應用程序的基本架構,通過與其他Spring框架組件集成,使…

    編程 2025-04-27
  • Mescroll.js——移動端下拉刷新和上拉加載更多組件

    一、概述 Mescroll.js是一款移動端的下拉刷新和上拉加載更多組件,因其簡單易用和功能強大而深受開發者的喜愛。Mescroll.js可以應用於各種移動端網站和APP,能夠支持…

    編程 2025-04-25
  • Vue強制重新渲染組件詳解

    一、Vue強制重新渲染組件是什麼? Vue中的強制重新渲染組件指的是,當我們需要重新渲染組件,但是組件上的數據又沒有改變時,我們可以使用強制重新渲染的方式來觸發組件重新渲染。這種方…

    編程 2025-04-25
  • Vue封裝公共組件的最佳實踐

    一、封裝公共組件的意義 隨着前端技術的不斷發展,Web應用程序變得越來越複雜。為了更好地管理和維護代碼,我們通常需要編寫可重用的組件,而這些組件往往是我們所寫的多個項目都需要用到的…

    編程 2025-04-25
  • ROS通信

    一、概述 ROS是機器人操作系統,是一個開源的、靈活的、分布式的軟件平台,可以幫助我們快速開發機器人應用程序。ROS中的通信是機器人應用程序開發中最重要的部分之一,它是實現多模塊協…

    編程 2025-04-25

發表回復

登錄後才能評論