vue父组件调用子组件方法(运用)

父组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<template>
  <div id="data-view">
    <dv-full-screen-container>
      <div class="mc-top">
        <water-level-chart :newData="data" ref="wlc"/>
      </div>
    </dv-full-screen-container>
  </div>
</template>
 
<script>
import waterLevelChart from './waterLevelChart'
 
export default {
  name: 'DataView',
  components: {
    waterLevelChart
  },
  data () {
    return {
      data: []
    }
  },
  methods: {
    get () {
      this.$http({ method: 'get', url: '/test/test' }).then(
        (resp) => {
          this.data = resp.data
          this.$refs.wlc.get(resp.data)
        }, (error) => {
          console.log('error=' + JSON.stringify(error))
        }).catch((exception) => {
      })
    }
  }
}
</script>

  子组件

复制代码
<template>
  <div id="water-level-chart">
    <div class="water-level-chart-title">FPY Summary {{newData.fpy}}%</div>
    <div class="chart-container">
      <dv-water-level-pond :config="config"/>
    </div>
  </div>
</template>

<script>
export default {
  props: ['newData'],
  data () {
    return {
      config: {
        data: [0],
        shape: 'round',
        waveHeight: 25,
        colors: ['rgb(133,165,255)'],
        waveNum: 2
      }
    }
  },
  methods: {
    get (newData) {
      const newConfig = {
        data: [newData.fpy],
        shape: 'round',
        waveHeight: 25,
        colors: ['rgb(133,165,255)'],
        waveNum: 2
      }
      this.config = newConfig
    }
  }
}
</script>

<style lang="less">

</style>
复制代码

1、在子组件中:<div></div>是必须要存在的 

2、在父组件中:首先要引入子组件 import waterLevelChart from './waterLevelChart';

3、 <water-level-chart ref="wlc"></water-level-chart>是在父组件中为子组件添加一个占位,ref="wlc"是子组件在父组件中的名字

 4、父组件中 components: {  是声明子组件在父组件中的名字

 5、在父组件的方法中调用子组件的方法,很重要   this.$refs.wlc.get("可传入值子组件接收");

posted @   想学前端的小李  阅读(405)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示