1. 程式人生 > >Vue 中父元件和子元件之間獲取對方資料和方法

Vue 中父元件和子元件之間獲取對方資料和方法

父元件獲取子元件的資料和方法

one

在父元件中呼叫子元件時,定義一個ref(其實和選擇器類似):

<Common  ref="commonChild" :pathologyId="form.pathologyId" />

在父元件中獲取子元件屬性和方法寫法:

this.$refs.commonChild.屬性

this.$refs.commonChild.方法


two

Vue官網上有這個例項屬性,但使用需要自定義元件順序

 


 

 子元件獲取父元件的資料和方法

在子元件中獲取父元件屬性和方法寫法:

this.$parent.屬性

this.$parent.方法

 


參考來源於:

https://cn.vuejs.org/v2/api/#vm-children

https://blog.csdn.net/zhongshijun521/article/details/80610971