1.父组件Home.vue

<template>
    <div>
        <h2>{{msg}}</h2>
        
        <v-header></v-header>
        
    </div>
</template>
<script>
import Header from "./Header.vue";
export default {
  name: 'home',  
  data () {
    return {
        msg:'首页组件',
        title:'父组件'
    }
  },
  methods:{
    run(){
      alert('父组件方法')
    }
  },
  components:{
    'v-header':Header
  }
}
</script>
<style lang="scss" scoped>
h2{
    color: red;
}
</style>

2.子组件Header.vue

<template>
    <div>
        <h2>{{msg}}</h2>
        <button @click="getData()">获取父组件数据</button>
        <button @click="getFunction()">获取父组件的方法</button>
    </div>
</template>
<script>
export default {
  name: 'Header',  
  data () {
    return {
        msg:'头部组件',
        title:'子组件'
    }
  },
  methods:{
      getData(){
          console.log(this.$parent.title)  
      },
      getFunction(){
          this.$parent.run()
      }
  }
}
</script>
<style lang="scss" scoped>
h2{
    color: green;
}
</style>

 

相关文章:

  • 2022-12-23
  • 2021-04-30
  • 2021-06-27
  • 2021-05-31
  • 2021-07-21
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-08-23
  • 2021-05-19
  • 2021-12-05
相关资源
相似解决方案