vue中,父组件主动获取子组件中的数据和方法及子组件主动获取父组件中的数据和方法

Vue4年前 (2020)更新 小马大哥哥
493 0 0

1.父组件主动获取子组件中的数据和方法

在父组件里面通过:

this.$refs.childMethod[0].属性
this.$refs.childMethod[0].方法

在父组件中:(调用子组件的时候,定义一个ref)

<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.$parent.属性
this.$parent.方法

在子组件中:

<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(){
			
		}
	}

 

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...