組件資料傳遞 Props (父傳子)
上層的 data 透過 props 往下傳,不會往上傳回 data,所以下層的資料改變不會影響到上層(data)
App.vue
1 | import PropsTest from "@/components/PropsTest.vue"; |
1 | <PropsTest :msg="data" /> // :子層屬性="父層 data" |
PropsTest.vue
1 | export default { |
1 | <h1>{{props.msg}}</h1> |
Props 型態
props 雖可以為陣列,但建議使用物件
表示,物件可以定義型別,還能設定預設值
1 | props: { |
Or
1 | props: { |
Props 定義各個型別的預設值
父層
1 | import PropsTest from "@/components/PropsTest.vue"; |
1 | <PropsTest |
子層
1 | export default { |
以上是我的學習筆記,希望也有幫助到你哦 😀