vue中,父組件主動獲取子組件中的數據和方法及子組件主動獲取父組件中的數據和方法。

1.父組件主動獲取子組件中的數據和方法javascript

在父組件裏面經過:html

this.$refs.childMethod.屬性
this.$refs.childMethod.方法

在父組件中:(調用子組件的時候,定義一個ref)java

<child-list ref="childMethod" :parentListClick="parent"></child-list>
<Button type="primary" @click="prentClick">點擊調用子組件方法</Button>
export default {
		data() {
			return {
				parent: '我是父組件中的屬性 !'
			}
		},
		methods: {
			prentClick() {
				this.$refs.childMethod.haizi();
				console.log(this.$refs.childMethod.child);
			},
			parentList(){
				console.log('我是父組件中的方法 !');
			}
		},
		created() {

		}
	}

 

 2.子組件主動獲取父組件中的數據和方法this

在子組件裏面經過:code

this.$parent.屬性
this.$parent.方法

在子組件中:htm

<Button type="primary" @click="childClick">點擊調用父組件方法</Button>
export default {
		props: ['parentListClick'],
		data(){
			return {
				child: '我是子組件中的屬性 !'
			}
		},
		methods: {
			haizi(){
				console.log('我是子組件中的方法 !');
			},
			childClick(){
				this.$parent.parentList();
				console.log(this.$parent.parent);
				console.log('------',this.parentListClick);
			}
		},
		created(){
			
		}
	}
相關文章
相關標籤/搜索