網(wǎng)站欄目劃分怎么做制作網(wǎng)頁的流程
在 Vue3 中,props
和 emit
的用法有所改變,主要的變化在于它們現(xiàn)在都通過 setup
函數(shù)來訪問和使用。
props:
props
用于父組件向子組件傳遞數(shù)據(jù)。在 setup
函數(shù)中,props
是一個(gè)參數(shù),我們可以通過它訪問到父組件傳入的所有 prop 值。
<script>
export default {props: ['message'],setup(props) {console.log(props.message) // 輸出父組件傳入的 message}
}
</script>
emit:
emit
用于子組件向父組件發(fā)送自定義事件。在 setup
函數(shù)中,emit
是第二個(gè)參數(shù),我們可以使用它來觸發(fā)自定義事件并傳遞數(shù)據(jù)。
<script>
export default {setup(props, { emit }) {const onClick = () => {emit('click', 'Hello, parent')}return { onClick }}
}
</script>
在這個(gè)示例中,當(dāng)點(diǎn)擊事件觸發(fā)時(shí),子組件會(huì)通過 emit
發(fā)送一個(gè)名為 ‘click’ 的事件,并傳遞一個(gè) ‘Hello, parent’ 的數(shù)據(jù)給父組件。父組件可以通過 v-on:click="handler"
或 @click="handler"
來監(jiān)聽這個(gè)事件。
以上是 Vue3 中 props
和 emit
的基本用法,更多的功能和詳情,你可以查閱 Vue 官方文檔 和 Vue 官方文檔 進(jìn)行學(xué)習(xí)。