<Parent></Parent> //父組件
<Child></Child> //子組件
父組件向子組件傳遞:
靜態傳遞(父組件動態改變,子組件不變):
<Parent :id='id'></Parent> //在父組件綁定定義參數 然后在子組件中添加: export default { props: ['id'], }
動態傳遞(父組件動態改變,子組件跟着變):
<Parent ref="child"></Parent> //在父組件定義ref,相當於為refs添加一個子組件名字 在子組件中定義方法,接收參數: childMethod(item){ this.item= item }, 然后在父組件JS中使用: this.$refs.child.childMethod(item) //將item參數傳給子組件
子組件向父組件傳遞:
<Parent @parentMethod="parentMethod"></Parent> //在父組件中定義個方法 在JS中添加方法: parentMethod(item) { this.item = item } 然后在子組件JS中: this.$emit('parentMethod','item') //這樣就將'item'字符串,傳給父組件this.item了