vue父子組件間傳值(props)


先定義一個子組件,在組件中注冊props

<template>
    <div>
        <div>{{message}}(子組件)</div>
    </div>
</template>
<script>
export default {
    props: {
        message: String  //定義傳值的類型
} } </script> <style> </style>

 

在父組件中,引入子組件,並傳入子組件內需要的值

<template>
    <div>
        <div>父組件</div>
        <child :message="parentMsg"></child>   
    </div>
</template>

<script>

import child from './child'  //引入child組件
export default {
    data() {
            return {
                parentMsg: 'a message from parent'  //在data中定義需要傳入的值
            }
        },
        components: {
            child
        }
}
</script>
<style>
</style>

 

這種方式只能由父向子傳遞,子組件不能更新父組件內的data

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM