1.父组件给子组件传值(方法)
1.父组件调用子组件,绑定动态属性
<v-header :title="title" :run="run"></v-header>
2.子组件通过pros接受父组件的传值
props:['title','run']
3.直接在子组件里使用
<h2> {{ title }}</h2>
<button @click="run()">父组件的方法</button>
2. 父组件主动获取子组件的数据方法
1.调用子组件定义ref
<v-header ref="header"></v-header>
2.父组件里面通过
this.$refs.header.属性
this.$refs.header.方法
3.父组件使用
<button @click="getChild()">获取子组件方法数据</button>
methods{
getChild(){
this.$refs.header.run();
}
}
3.子组件主动获取父组件的数据方法
子组件中使用:
this.$parent.数据
this.$parent.方法
<button @click="getParent()"> 获取父组件</button>
methods:{
getParent(){
alert(this.$parent.msg);
this.$parent.run();
}
}