Vue父组件调用子组件方法(栗子)

ref 加在子组件上,用this.ref.子类name 获取到的是组件实例,可以使用组件的所有方法。!!!

项目结构

APP.vue

复制代码
<template>
  <div id="app">
    <datav />
  </div>
</template>

<script>
import datav from './components/test/index'
// import datav from './components/datav/index.vue'
// import datav from './components/offlineborad/index.vue'
// import datav from './components/offlineborad1/index.vue'
// import datav from './components/offlineborad2/index.vue'
// import datav from './components/offlineborad3/index.vue'
// import datav from './components/offlineborad4/index.vue'
// import datav from './components/offlineborad5/index.vue'
export default {
  name: 'app',
  components: {
    datav
  },
  data () {
    return {}
  }
}
</script>

<style lang="less">
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  width: 100%;
  height: 100%;
}
</style>
复制代码

index.vue

复制代码
<template>
    <div>
      <el-button @click="get" >父类按键加一</el-button>
      <son ref="son"></son>
    </div>
</template>

<script>
import son from './son'
export default {
  components: {
    son
  },
  name: 'index',
  methods: {
    get () {
      this.$refs.son.big()
    }
  }
}
</script>

<style scoped>

</style>
复制代码

son.vue

复制代码
<template>
    <div>
      子类数值{{count}}
    </div>
</template>

<script>
export default {
  name: 'son',
  data () {
    return {
      count: 0
    }
  },
  methods: {
    big () {
      this.count++
    }
  }
}
</script>

<style scoped>

</style>
复制代码

 

posted @   想学前端的小李  阅读(80)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示